2

read final edit for solution

I'm try update the onclick function of a link using jquery.

example would be best to explain:

html

<a href='' id='one_click' onclick='do_things_here("fighter","bitter",2)'>first clicker</a>

<a href='' id='two_click' onclick='do_things_here("abc","xyz",1)'>second clicker</a>

js (something like this - I don't this is actually correct logically speaking)

function do_thing_here(data, info, source){

  *//things happen here*

  $('#two_click').click(function() {
    do_things_here(data,info,source)
    return false;

    });


}

That doesn't work as it goes into a recursive loop as do_things_here gets set off when resetting the onclick on 'two_click'.

edit

I've tried to set a reset flag to prevent do_things_here running on re-setting the onclick

function do_thing_here(data, info, source,reset){
      if (reset){
           return false;
      }


      *//things happen here*

      $('#two_click').click(function() {
        do_things_here(data,info,source,true)
        return false;

        });


    }

but that did seems a bit hack-ish and I don't think it worked, I still had issues - I'll try to do it again and see what the issues where.

I've tried unbinding and removeAttr/attr in an attempt to reassign as explained here

JavaScript: changing the value of onclick with or without jQuery

those methods do not seem to work in all browsers

  • Using jQuery attr("onclick", js) doesn't work with both Firefox and IE6/7.
  • Using setAttribute("onclick", js) works with Firefox and IE8, but not IE6/7.
  • Using onclick = function() { return eval(js); } doesn't work because you are not allowed to use return is code passed to eval().

I've tried clearing out the onclick function in the html so nothing is set as default but that didn't have any effect.

I can simply reset/rejig the link as follows

<span id='change_two'><a href='' id='two_click' onclick='do_things_here("abc","xyz",1)'>second clicker</a></span>

and then

 function do_thing_here(data, info, source){

      *//things happen here*

      $('#change_two').html("<a href='' id='two_click' onclick='do_things_here('+data+','+info+','+soutce+')'>second clicker</a>");


    }

or something similar but I'm wondering if there is a better cleaner way to do it?

----edit:solution explained----

just for those googling on to this page:

if you set the onclick via html you need to removeAttr ($(this).removeAttr('onclick'))

if you set it via jquery (as the after the first click in my examples above) then you need to unbind ($(this).unbind('click'))

Then you simply rebind with bind ($(this).bind('click',function(event){//the code/function call})))

//important - removeAttr: use 'onclick' and unbind: use 'click' - I think that was my main problem!

wasn't sure if I should post this as an answer as the solution was right there in my question! Sorry folks for the brain failure.

I know in the question it states "not using unbind/removeAttr" - that was because I thought it was not working with those functions - but that was only due to the usage of the different functions.

2 Answers 2

2

Not sure what you are trying to achieve, but to unbind a click event, just use $(this).unbind('click')

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

4 Comments

I would say that's the most concise way, but the OP states he does not want to use it.
Let me have another go, I tried unbind/removeAttr/bind and it did not seem to work in FF but maybe I wasn't paying attention. brb
Grr, My brain!, use unbind() - if you bind with jquery (and removeAttr if set via html) then use bind(click,function(event){//you code here}). I did this all day and it wouldn't work and now, to show me up, it works! thanks rkw just to get me thinking again!
i've edited an explanation to the question for those who want more detail.
0

Summarized answer that I used successfully,

if you set the onclick via html you need to removeAttr ($(this).removeAttr('onclick'))

if you set it via jquery (as the after the first click in my examples above) then you need to unbind ($(this).unbind('click'))

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.