Reputation: 2646
I have a script that enables my uses to block / unblock users. They're both in the same function 'blockToggle(type, user, button)' and depending on the type( either block or unblock) it executes that script. But what I'm trying to do is if the current user doesnt have another user blocked, they can block them with the button, but when the user clicks the button, they have to refresh the page to unblock the user. I want the onclick attribute to change right away once the user clicks it without refreshing the page. Here are a few things i've tried:.
document.getElementById("blockBtn").onclick = "blockToggle('unblock', 'user', 'blockBtn')";
document.getElementById("blockBtn").setAttribute( "onClick", "javascript: blockToggle('unblock', 'user', 'blockBtn')";
document.getElementById("blockBtn").onclick = function(){ "blockToggle('unblock', 'user', 'blockBtn')"};
Neither one of these solutions worked for me. And I couldn't seem to find a solution from google search either. Is there anything I'm overseeing here? I'm sure the answers right in front of me.
Upvotes: 0
Views: 4042
Reputation: 92274
Stay away from setting handlers as strings. Just use a normal anonymous function that calls your blockToggle
.
document.getElementById("blockBtn").onclick = function(){
blockToggle('unblock', 'user', 'blockBtn');
};
Upvotes: 0
Reputation: 28837
I dont know how your function blockToggle()
looks like, but try this, without the extra quotes:
document.getElementById("blockBtn").onclick = function(){
blockToggle(unblock, user, blockBtn);
};
I am assuming your function blockToggle
is declared and that unblock
user
& blockBtn
are declared variables also.
Upvotes: 1