JavaScript: changing the value of onclick with or without jQuery -


I want to change the value of the onclick attribute on an anchor. I set it in a new string I want to do that includes javascript. (This string server is provided by the client-side javascript code, and can include whatever you can put in the HTML in the onclick attribute.) Here are some things I tried By using setAttribute (<">

  • Use the setAttribute (" onclick ", js ) does not work with both Firefox and IE 6/7. Onclick ", js) works with Firefox and IE8, but IE 6/7 does not work.
  • onclick = function () {return eval (js); } does not work because you do not have permission to use the return eval () .
  • Does firefox and IE 6/7/8 suggest anybody to set the onclick feature to work on this? Also see below the code I used to test this.

      & lt; Html & gt; & Lt; Top & gt; & Lt; Script type = "text / javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; $ (Document) .ready (function () {var js = "alert ('b'); return false;"; // Set with jquery: $ ("a") does not work. Attr ("onclick" , Js); // Set with setAttribute (): At least works with Firefox http: //document.getElementById ("anchor") .setAttribute ("click", js);}); & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; A href = "http://www.google.com/" id = "anchor" onclick = "warning ('a'); return false;" & Gt; Click & lt; / A & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

    You should not use onClick JQuery Attached and Binding offers your methods of events. See

      $ (document) .ready (function () {var js = "alert ('B:' + this.id); return false;"; // "js" string Var newclick = New function (JS); // click on the ClearOr using jQuery $ ("#Anchor"). Click on ('onclick', ''). Click (New click);));  

    This should cancel the onClick function - as well as "String to Javascript".

    The best way to remove the code from the onclick = "" and ATC element in the HTML code and click to force the event to be clicked Switch to use.

    You also said:

    using onclick = function () {return eval (js); } does not work because you do not have permission to use the return passed code in eval ().

    No - this will not happen, but onclick = eval ("(function () {" + js + "})"); The function wrapping the 'JS' variable in the enclosure I onclick = new function (js); also works and is a bit cleaner to read (note capital F) - See


    Comments

    Popular posts from this blog

    python - Overriding the save method in Django ModelForm -

    html - CSS autoheight, but fit content to height of div -

    qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -