PDA

View Full Version : JQuery - Get the ID of a clicked item.



denizengt
August 15th, 2007, 11:21 PM
Hey there all, I have a list of items with the class click, ie


<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



$(".click").click(function(){
//Get the id of this clicked item

pos = this.attr("id");
goPos(pos);
});

ratherblue
August 16th, 2007, 01:16 AM
this.id

ratherblue
August 16th, 2007, 01:26 AM
to give more clarity:



<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>

ratherblue
August 16th, 2007, 01:30 AM
well crap, i thought jquery was just your fancy word for javascript... turns out jquery is something completely different :( sorry

denizengt
August 16th, 2007, 01:52 AM
Actually, you're right




$(".click").click(function(){
//Get the id of this clicked item

pos = this.id;

goPos(pos);
});


Gold. Thanks mate !

numediaweb
December 11th, 2009, 02:42 PM
here you are, change script to fit;


<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>

simplistik
December 11th, 2009, 05:08 PM
2 year old post brotha

JoshOrndorff
September 1st, 2010, 06:04 PM
2 year old post brotha

That's okay, it just helped me today and, based on its pagerank, it has probably helped others :)

-Joshy

etoileweb
February 3rd, 2012, 03:47 AM
Too good. It actually helped me so much. I've been searching on the web for two days now.

etoileweb
February 3rd, 2012, 03:48 AM
Bookmarked

luisferfranco
April 1st, 2012, 12:42 AM
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.

Jam7
May 17th, 2012, 02:17 AM
pos = $(this).attr("id");