locked
How to add a click / tap event in JavaScript.. Or do I even need to

    Question

  • This should be an easy one..

    I've got a series of divs on my page that contains an image and text. Similar to a figure/figurecaption. I want to be able to add some sort of an event to the div so that whenever the div, or anything in it is pressed via a mouse click or a tap, the event fires.

    This is super easy if it's a button, and it's easy enough in C# / XAML. I'm trying to challenge myself with Html5.

    I can already loop through the divs with a GetELementsByClass call. I'm getting the references to each div, but I'm not sure of the event name.  Divs, ordinarily, don't have a click event, right? I tried adding the event handler of onClick, onclick, click, etc. They don't appear to work.

    What's the best way to accomplish what I need to do? Is there something I'm doing wrong?

    -J

    Friday, November 16, 2012 10:10 PM

Answers

  • I think I found out what I need to do.

    I can simply use a <p> tag , a </p> tag and handle the onclick there.

    • Marked as answer by FearTheDonut Saturday, November 17, 2012 8:06 PM
    Saturday, November 17, 2012 8:06 PM

All replies

  • button1.addEventListener("click", button1Click, false);


    http://zSprawl.com

    Saturday, November 17, 2012 4:28 AM
  • I understand the button event, yes. But that's not what I'm asking.

    Consider my Div, that I want to function as a button. It has an image, text, background, etc. How do I add a "click" event to it? It isn't a button, etc. I'll look more into seeing how I might be able to customize a button, to look like my div, and maybe this will address the issue.

     

    Saturday, November 17, 2012 7:48 PM
  • I think I found out what I need to do.

    I can simply use a <p> tag , a </p> tag and handle the onclick there.

    • Marked as answer by FearTheDonut Saturday, November 17, 2012 8:06 PM
    Saturday, November 17, 2012 8:06 PM