admin管理员组

文章数量:1394211

I have an HTML anchor tag like

<a href="anchorid" onclick="callEvent(1)">

Here I call the Javascript function like

<script>
function callEvent(anchor) {
    alert("Anchor ID is - "+anchor);
    document.getElementById("anchorid").onClick = function () { callEvent(0) }; // BY using this code, update the onclick callEvent(0), like toggle
}
</script>

I wants to update the anchor tag like

<a href="anchorid" onclick="callEvent(0)">

When using this code, it is not updating as per my requirement.

 document.getElementById("anchorid").onClick = function () { callEvent(0) };

How do I get it to update?

I have an HTML anchor tag like

<a href="anchorid" onclick="callEvent(1)">

Here I call the Javascript function like

<script>
function callEvent(anchor) {
    alert("Anchor ID is - "+anchor);
    document.getElementById("anchorid").onClick = function () { callEvent(0) }; // BY using this code, update the onclick callEvent(0), like toggle
}
</script>

I wants to update the anchor tag like

<a href="anchorid" onclick="callEvent(0)">

When using this code, it is not updating as per my requirement.

 document.getElementById("anchorid").onClick = function () { callEvent(0) };

How do I get it to update?

Share Improve this question edited Feb 6, 2014 at 15:01 Jon Adams 25.2k18 gold badges84 silver badges121 bronze badges asked Sep 12, 2013 at 8:54 user2688251user2688251 1
  • PHP tag wasn't necessary here, i've removed it. – STT LCU Commented Sep 12, 2013 at 8:59
Add a ment  | 

7 Answers 7

Reset to default 2

for using document.getElementById("anchorid").. you need to have id in your element, which you currently dont have, try doing:

//add id to your anchor
<a href="javascript:void(0);" id="anchorid" onclick="return callEvent(1);">
    test
</a>

and js

<script type="text/javascript">
function callEvent(num) {
    alert("Anchor ID is - "+num);
    document.getElementById('anchorid').onclick = function () { callEvent(0) }; 
}
</script>

Store the toggle value in tag itself and then use.

<script>
function callEvent(val) {
    var val = document.getElementById("myId").getAttribute('data-val');
    alert(val);
    // toggle value
    val = val==1?0:1;
    document.getElementById("myId").setAttribute('data-val',val);
}
</script>
<a id="myId" data-val="0" onClick="callEvent()">Click</a>

here value is stored in data-val value so that is toggled in the callEvent function itself, so no need to rebind the event.

See Example fiddle

try:

document.getElementById("anchorid").onclick

Better:

document.getElementById("anchorid").addEvenetListener('click', function(){

});

Tested: http://jsfiddle/Yca5W/

document.getElementById("anchorid").onclick =
    function () {
        alert("clicked");
    };

if you only change parameter of calling function then you dont have to change plete event. You can do it like this:

HTML

<a id="anchorid" href="#">click me !</a>

JS

<script>
var anchor = 1;

document.getElementById("anchorid").onclick = function(){
    alert("Anchor ID is: " + anchor);
    anchor = anchor == 1 ? 0 : 1; //toggle 0 and 1
}
<script>

try This:

var anchor= document.getElementById('anchorid');
  anchor.addEventListener('click', function() {
        callEvent(0);
    });

OR

$( "#anchorid" ).click(function() {
 callEvent(0);
});

if you only want changes passing parameter from 1 to 0 or vice verse then do this one:

 <input type="hidden" name="para" id="para" value="1" > 
    <a href="anchorid" onclick="callEvent($('#para').val())">



 $( "#anchorid" ).click(function() {
      if ($('#para').val()==1) {
        $('#para').val(0)
      } else {
        $('#para').val(1)
      }
    });

try this, it may solve your problem demo Fiddle

Add id="anchorid" to your anchor tag

When you click it next time it will callEvent by argument 0,

function callEvent(anchor) {
    alert("Anchor ID is - "+anchor);
    var ele =  document.getElementById("anchorid");
    ele.setAttribute("onclick","callEvent(0)");

}

It will update your link like you wanted

<a href="anchorid" id="anchorid" onclick="callEvent(0)">

本文标签: htmlChange the OnClick event using JavascriptStack Overflow