24

I want to make my script set the onclick properity of a <div>.

I use this Html code:

<div id="forgotpass">Forgot Password?</div>

I want when a user clicks the <div> a forgotpass() function to run, but I do not want to use this:

<div id="forgotpass" onclick="forgotpass();">Forgot Password?</div>
0

8 Answers 8

55

Alternatively, if you're not using jQuery:

document.getElementById('forgotpass').onclick = forgotpass;
Sign up to request clarification or add additional context in comments.

2 Comments

245 rep in a day so far? KILLER! And +1 for the objective answer.
this helped me solve an issue. i was trying to do something like document.getElementById('forgotpass').onclick = forgotpass(); but you pointed out that the brackets should not be included- thanks
29

Pure JavaScript:

function addListener(element, eventName, handler) {
  if (element.addEventListener) {
    element.addEventListener(eventName, handler, false);
  }
  else if (element.attachEvent) {
    element.attachEvent('on' + eventName, handler);
  }
  else {
    element['on' + eventName] = handler;
  }
}

function removeListener(element, eventName, handler) {
  if (element.addEventListener) {
    element.removeEventListener(eventName, handler, false);
  }
  else if (element.detachEvent) {
    element.detachEvent('on' + eventName, handler);
  }
  else {
    element['on' + eventName] = null;
  }
}

addListener(document.getElementById('forgotpass'), 'click', forgotpass);

jQuery:

$(document).ready(function() {
  $("#forgotpass").click(forgotPass);
});

Or:

$(document).ready(function() {
  $("#forgotpass").click(function() {
    forgotPass();
  });
});

1 Comment

When using jQuery it's best if done as following, if the said code is executed more than once - there could be a practical scenario. Otherwise, multiple eventhandles will be registered $("#forgotpass").off().on('click', function () { forgotPass(); });
3

Something like this might work..

var div = document.getElementById("forgotpass");
div.onclick=function(){ /*do something here */ };

if you dont add the function, the javascript will run the onclick once it runs through the script.

1 Comment

This is the best answer because allows you to call a function and also pass values
2

You can do it with jQuery like

$("#forgotpass").click(function() {
  alert("Handler for .click() called.");
});

Comments

2

In pure javascript you can do:

function forgotpass() {
 //..code
}

var el = document.getElementById("forgotpass");
el.onclick = forgotpass;

but this is very naive, not flexible and probably a bad practice.

If you are using jQuery, you can do:

function forgotpass() {
 //..code
}

$(document).ready(function() {
  $("#forgotpass").click(function() {
    forgotPass();
  });
});

2 Comments

why is it naive and bad practice exactly?
I don't know about naive or bad practice, but the highest ranked and accepted answer's preference for addEventListener over onclick is backed up by developer.mozilla.org/en-US/docs/Web/API/EventTarget/… which describes why: unlike onclick, addEventListener can capture the event as it gets passed down the DOM, prevent it from bubbling back up the DOM, and can be used to add multiple event handlers.
2

If you only need to support IE 9+ (source), you can use EventTarget.addEventListener in pure JavaScript.

function forgotpass() {
  alert("Hello, world!");
}

var element = document.getElementById("forgotpass");
element.addEventListener("click", forgotpass, false);
<button id="forgotpass">Forgot Password?</button>
If you need to support older browsers, I recommend Speransky Danil's answer.

Comments

0

Adding event-listner directly in the HTML :

...
<div>
    <input type="button" value="Set Cookie" onclick="setCookie();" />
</div>
<script>
    function setCookie() {
        console.log('ready to set cookie?');
    }
</script>
...

Reference : W3CSchools

Good Luck!

Comments

0

If you are using jQuery it's best if done as follows. If the function call is executed more than once multiple eventhandles will be registered. Following approach makes sure the previous handlers are removed

$("#forgotpass").off().on('click', function () { 
    forgotPass(); 
});

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.