SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    div into the div

    Simple example:

    <div style="background-color: #ff0000; width: 300px; height: 200px" onclick="test(this)" id="test">
    TEST <br><br>

    <div style="background-color: #000ff0; width: 200px; height: 100px" onclick="test(this)" id="hello">
    Hello!!!
    <div>
    </div>

    <script>
    function test(obj) {
    alert(obj.id);
    }
    </script>

    When I click on the blue div I see two alerts "hello" and "test". When I click on the 'blue' div I need to get the alert only from the blue div (from the higher layer).

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Before we get started, I'm going to simplify the code by moving the style information out of the html.

    Code CSS:
    #test {
    	background-color: #ff0000;
    	width: 300px;
    	height: 200px;
    }
    #hello {
    	background-color: #000ff0;
    	width: 200px;
    	height: 100px;
    }

    Because you don't want the click activating the parent div, you need to stop the click event from bubbling up through the elements.

    To do that, pass the event as an argument as well, then use that event to cancel any other behaviour.

    Code html4strict:
    <div onclick="test(event, this)" id="test">
        TEST <br><br>
        <div onclick="test(event, this)" id="hello">
            Hello!!!
        <div>
    </div>

    Code javascript:
    function test(evt, obj) {
    	evt = evt || window.event;
    	alert(obj.id);
    	evt.cancelBubble = true; // IE
    	if (evt.stopPropagation) {
    		evt.stopPropagation(); // W3C
    	}
    }

    you can get rid of that obj argument too, by getting the target element from the event.

    Code html4strict:
    <div onclick="test(event)" id="test">
        TEST <br><br>
        <div onclick="test(event)" id="hello">
            Hello!!!
        <div>
    </div>

    Code javascript:
    function test(evt) {
    	evt = evt || window.event;
    	targ = evt.target || evt.srcElement;
    	alert(targ.id);
    	evt.cancelBubble = true; // IE
    	if (evt.stopPropagation) {
    		evt.stopPropagation(); // W3C
    	}
    }

    You can even get rid of the inline javascript and assign the event from javascript, where the event is automatically passed as the first argument.

    Code html4strict:
    <div id="test">
        TEST <br><br>
        <div id="hello">
            Hello!!!
        <div>
    </div>

    Code javascript:
    function test(evt) {
    	evt = evt || window.event;
    	targ = evt.target || evt.srcElement;
    	alert(targ.id);
    	evt.cancelBubble = true; // IE
    	if (evt.stopPropagation) {
    		evt.stopPropagation(); // W3C
    	}
    }
    onload = function () {
        document.getElementById('test').onclick = test;
        document.getElementById('hello').onclick = test;
    }

    And if you place your scripts at the end of he body just before the </body> it's even easier to attach the events.

    Code javascript:
    function test(evt) {
    	evt = evt || window.event;
    	targ = evt.target || evt.srcElement;
    	alert(targ.id);
    	evt.cancelBubble = true; // IE
    	if (evt.stopPropagation) {
    		evt.stopPropagation(); // W3C
    	}
    }
    document.getElementById('test').onclick = test;
    document.getElementById('hello').onclick = test;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •