Results 1 to 4 of 4

Thread: click function problem

  1. #1
    Join Date
    Dec 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default click function problem

    InJQuery , when a html element is clicked , I want to pass the CLASS value of this HTML element to the click(function(){ //blah blah }

    How to write that ?

  2. #2
    Join Date
    Oct 2009
    Posts
    845
    Thanks
    14
    Thanked 189 Times in 188 Posts

    Default

    I'm having difficulties understanding the question. Can you say some more about what you want to achieve ?

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In jQuery the click function is like - say the element has an id of 'myDiv':

    Code:
    $('#myDiv').click(function(){alert('You clicked me!');});
    You can select it many ways - say you want all a tags:

    Code:
    $('a').click(function(){alert('You clicked me!');});
    Now the class name could be important. Inside the click function it would be:

    this.className

    So you could do:

    Code:
    $('a').click(function(){alert(this.className);});
    or even:

    Code:
    $('a').click(function(){if(this.className == 'active'){
    			doSomething();
    		} else {
    			doSomethingElse();
    		}
    	});
    In fact, since an element can have more than one class name, you can use jQuery's hasClass. But then you have to make 'this' into a jQuery object, like:

    Code:
    $('a').click(function(){if($(this).hasClass('active')){
    			doSomething();
    		} else {
    			doSomethingElse();
    		}
    	});
    If you need more specific help, please explain the specifics, and:

    Please post a link to a page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    azoomer (07-01-2010)

  5. #4
    Join Date
    Dec 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    In jQuery the click function is like - say the element has an id of 'myDiv':

    Code:
    $('#myDiv').click(function(){alert('You clicked me!');});
    You can select it many ways - say you want all a tags:

    Code:
    $('a').click(function(){alert('You clicked me!');});
    Now the class name could be important. Inside the click function it would be:

    this.className

    So you could do:

    Code:
    $('a').click(function(){alert(this.className);});
    or even:

    Code:
    $('a').click(function(){if(this.className == 'active'){
    			doSomething();
    		} else {
    			doSomethingElse();
    		}
    	});
    In fact, since an element can have more than one class name, you can use jQuery's hasClass. But then you have to make 'this' into a jQuery object, like:

    Code:
    $('a').click(function(){if($(this).hasClass('active')){
    			doSomething();
    		} else {
    			doSomethingElse();
    		}
    	});
    If you need more specific help, please explain the specifics, and:

    Please post a link to a page on your site that contains the problematic code so we can check it out.
    Thanks for the post. Thanks for your time.
    You are a very nice person . You explained beautifully.
    It was very much helpful to me. I have understood the concept now.

    Thanks

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
  •