javascript - Updating onclick function within another function not using unbind/removeAttr -


read final edit solution

i'm try update onclick function of link using jquery.

example best 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 - don't 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 goes recursive loop do_things_here gets set off when resetting onclick on 'two_click'.

edit

i've tried set reset flag prevent do_things_here running on re-setting 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 did seems bit hack-ish , don't think worked, still had issues - i'll try again , see issues where.

i've tried unbinding , removeattr/attr in attempt reassign explained here

javascript: changing value of onclick or without jquery

those methods not seem work in browsers

  • using jquery attr("onclick", js) doesn't work both firefox , ie6/7.
  • using setattribute("onclick", js) works firefox , ie8, not ie6/7.
  • using onclick = function() { return eval(js); } doesn't work because not allowed use return code passed eval().

i've tried clearing out onclick function in html nothing set default didn't have effect.

i can reset/rejig link 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 similar i'm wondering if there better cleaner way it?

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

just googling on page:

if set onclick via html need removeattr ($(this).removeattr('onclick'))

if set via jquery (as after first click in examples above) need unbind ($(this).unbind('click'))

then rebind bind ($(this).bind('click',function(event){//the code/function call})))

//important - removeattr: use 'onclick' , unbind: use 'click' - think main problem!

wasn't sure if should post answer solution right there in question! sorry folks brain failure.

i know in question states "not using unbind/removeattr" - because thought not working functions - due usage of different functions.

not sure trying achieve, unbind click event, use $(this).unbind('click')


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -