[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


div onclick

Posted on 2006-05-28
Medium Priority
Last Modified: 2006-11-18
Hi All,
I have a div like this -->

<div id="div_test" style="width:200px; background-color:#AAAAAA" onclick="location.href='aaaa.html';">
    <img src="images/spacer.gif" width="20"> this is the content

whenever user clicks the colored area, it will redirect him to aaaa.html
how can i do sth. like this --> if the user cilcks "the spacer.gif", the page won't be directed?

thanks a lot!
Question by:orbulat
LVL 10

Accepted Solution

0h4crying0utloud earned 1000 total points
ID: 16782024

I would use a separate event handler that find the target. note: The event handler assignment has to be made after the body tag.


<div id="div_test" style="width:200px; background-color:#AAAAAA" >
    <img id="img_test" src="spacer.jpg" width="20"> this is the content



function getTarget(e) {
      var target;
      if (!e) var e = window.event;
      if (e.target) target = e.target;
      else if (e.srcElement) target = e.srcElement;
      if (target.nodeType == 3) // defeat Safari bug
            target = target.parentNode;
      return target;

function handleOnClick(e) {
  var targetObj = getTarget(e);
      if (targetObj.id != "img_test") {
  return false;

document.getElementById("div_test").onclick = handleOnClick;

LVL 10

Author Comment

ID: 16782089
thanks a lot!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question