Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 12 of 12

Thread: JQuery - Get the ID of a clicked item.

  1. #1

    JQuery - Get the ID of a clicked item.

    Hey there all, I have a list of items with the class click, ie
    Code:
    <a class = "click" id = "1">Item 1</a>
    <a class = "click" id = "2">Item 2</a>
    <a class = "click" id = "3">Item 3</a>
    <a class = "click" id = "4">Item 4</a>
    and so on - these items are created dynamically.

    Now how can I use jquery to return the ID's of whatever one I click on?

    I've tried this, but it doesn't work

    Code:
    $(".click").click(function(){
                                       //Get the id of this clicked item
                               
                                      pos = this.attr("id");
                                       goPos(pos);
                                       });

  2. #2
    this.id

  3. #3
    to give more clarity:

    Code:
    <script type="text/javascript">
    function getID(theLink){
    
    	alert(theLink.id);
    
    
    }
    
    </script>
    
    
    <a id="1" href="#" onclick="javascript:getID(this)">Test</a>
    <a id="2" href="#" onclick="javascript:getID(this)">Test</a>

  4. #4
    well crap, i thought jquery was just your fancy word for javascript... turns out jquery is something completely different sorry

  5. #5
    Actually, you're right


    Code:
    $(".click").click(function(){
                                       //Get the id of this clicked item
                               
                                      pos = this.id;
    
                                       goPos(pos);
                                       });
    Gold. Thanks mate !

  6. #6

    dynamic jquery

    here you are, change script to fit;
    Code:
    <a class="preset_text" id="1">model 1</a>
    <a class="preset_text" id="2">model 2</a>
    
    <textarea name="editor" cols="50" rows="17" id="editor">Just type.</textarea>
    
    <input type="hidden" value="ceci est le contenue de l'example 1" id="link_content_1"/>
    <input type="hidden" value="ceci est le contenue de l'example 2" id="link_content_2"/>
    
    
    <script type="text/javascript">
    	$(document).ready(function() {
    		$('.preset_text').click(function(){			
    			var target = $(this).attr("id");			
    			$('textarea#editor').val($('#link_content_'+target).val());
    			<!--alert(target);-->
    		});
    	});
    </script>
    I'll make him an offer he can't refuse...

  7. #7
    2 year old post brotha
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  8. #8
    Quote Originally Posted by simplistik View Post
    2 year old post brotha
    That's okay, it just helped me today and, based on its pagerank, it has probably helped others

    -Joshy

  9. #9
    etoileweb
    n/a
    posts
    Guest
    Too good. It actually helped me so much. I've been searching on the web for two days now.

  10. #10
    etoileweb
    n/a
    posts
    Guest
    Bookmarked

  11. #11
    I just register into this forum to thank you both of you guys, for the way you asked the question, and then how it was answered.

    Thanks a lot.

  12. #12
    Jam7
    n/a
    posts
    Guest

    alternative

    pos = $(this).attr("id");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012