0

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.

5
  • You have a syntax error in your code. Look how the code is highlighted. Commented Sep 14, 2013 at 18:37
  • Are unblock, user, and blockBtn supposed to be literal strings or variable names? If they're variables, you shouldn't put them in quotes. Commented Sep 14, 2013 at 18:43
  • the only one thats a variable is user, which in my actual code i don't have them it quotes Commented Sep 14, 2013 at 18:54
  • I actually think it was a syntax error in my code :O i just deleted it all and wrote it again and using the .setAttribute function in javascript worked. Commented Sep 14, 2013 at 19:07
  • If you just had a typo error please delete this question. Commented Sep 14, 2013 at 20:54

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think unblock, user and blockBtn are strings
0

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');
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.