10

I want to add an event handler to a paragraph for when any user clicks on it. For example, I have a paragraph which would show an alert when a user clicks it, but without using "onclick" on HTML.

 <p id="p1">This is paragraph Click here..</p>
 <a href="http://www.google.com" id="link1" >test</a>
 document.getElementById('p1').onmouseover  = paragraphHTML; 
1
  • <p id="p1">This is paragraph Click here..</p> <br /> <a href="google.com" id="link1" >test</a> <script type="text/javascript"> document.getElementById('p1').onmouseover = paragraphHTML; // no parentheses in paragraphHTML function paragraphHTML() { alert(this.innerHTML); } </script> Commented Mar 21, 2012 at 7:33

3 Answers 3

25

You can add event listener.
Smth. like this:

 var el = document.getElementById("p1");
if (el.addEventListener) {
        el.addEventListener("click", yourFunction, false);
    } else {
        el.attachEvent('onclick', yourFunction);
    }  

(thanks @Reorx)

Explanation Here

Complete code (tested in Chrome&IE7):

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
        <script type="text/javascript">
            window.onload =function (){
            var el = document.getElementById("p1");
            if (el.addEventListener) {
                el.addEventListener("click", yourFunction, false);
            } else {
                el.attachEvent('onclick', yourFunction);
            }
            };
            function yourFunction(){
                alert("test");
            }
        </script>
    </head>
    <body>
        <p id="p1">test</p>

    </body>
</html>
Sign up to request clarification or add additional context in comments.

7 Comments

Hi its working in firefox but Ie there is a problem its says Object doesn't support this property or method
you said you don't want "onclick" event to p1. Did u? el.addEventListener("click", yourFunction, false) is adding event to "onclick"
@Jun1st: mmm. you are right. as far as I understood adilahmed just didn't want it to be inline
Hi all i did like this for work in both firefox and Ie but its only woking in Firefox
@adilahmed: works perfect in Chrome. What error do you get? Where do you add the script? Inside window.onload function?
|
6

To suit most situations, you can write a function to handle this:

var bindEvent = function(element, type, handler) {
    if (element.addEventListener) {
        element.addEventListener(type, handler, false);
    } else {
        element.attachEvent('on'+type, handler);
    }
}

1 Comment

In IE this function will find the element for binding has no attribute 'addEventListener', then the handler will be assigned to its 'onTYPE' attribute, eg, 'onclick'.
1

Add a tabIndex attribute to your p element, then you can use the onfocus function.

demo: http://jsfiddle.net/9y7CL/

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.