Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    64
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question even.target help

    hi sirs i wonder why my code isn't working whenever i changed it to even.target and add an "if" control struc

    here's my original html code
    Code:
    <div id="switcher">
    <h3>Style Switcher</h3>
    <button type="button" class="button" id="switcher-default">Default</button>
    <button type="button" class="button" id="switcher-narrow">Narrow Column</button>
    <button type="button" class="button" id="switcher-large">Large Print</button>
    </div>
    <div class="text">testing testing testing</div>
    the css
    Code:
    .hidden{
      display: none;
    }
    
    .button{
      padding: 0.2em;
      background-color: #FFFFFF;
      border: 1px outset #000000;
    }
    and here's the jquery that works and hides the 3 buttons
    Code:
    $(document).ready(function(){
      $('#switcher').click(function(){
          $('#switcher .button').toggleClass('hidden');
      });
    });
    but when i changed the jquery code to
    Code:
    $(document).ready(function(){
      $('#switcher').click(function(event){
        if(event.target == this){
          $('#switcher .button').toggleClass('hidden');
        }
      });
    });
    it doesn't work anymore, can someone tell me what's wrong with it ?

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Code:
    $('#switcher').click(function(event){
    	
    	console.log(event.target);
    	console.log(this);
    		
    	if(event.target == this){
    		$('#switcher .button').toggleClass('hidden');
    	}
    });
    Most of the time when I was testing it, it's because you're usually clicking on the h3 rather than the #switcher div.

  • Users who have thanked Spudhead for this post:

    capt_nemo777 (03-03-2010)

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    64
    Thanks
    12
    Thanked 0 Times in 0 Posts
    thank you sir, i didn't notice that


  •  

    Posting Permissions

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