If I have id's of the form: t_*
where * could be any text, how do I capture the click event of these id's and be able to store the value of * into a variable for my use?
views:
45answers:
4
A:
var CurrentID = $(this).attr('id');
var CurrentName = CurrentID.replace("t_", "");
That should help with the repalce.
Pino
2010-08-20 12:29:13
replace("t_", ""); does not work with id of "t_myidt_endit" as that gives "myidendit" not "myidt_endit"
Mark Schultheiss
2010-08-20 13:00:52
+3
A:
Use the starts with selector ^=
like this:
$('element[id^="t_"]').click(function(){
alert($(this).attr('id'));
});
Where element
could be any element eg a div
, input
or whatever you specify or you may even leave that out:
$('[id^="t_"]').click(function(){
alert($(this).attr('id'));
});
Update:
$('[id^="t_"]').click(function(){
var arr = $(this).attr('id').split('_');
alert(arr[1]);
});
More Info:
Sarfraz
2010-08-20 12:29:51
Well not exactly precise try the id string "t_myt_endit". See my answer for getting just the beginning of the ID attribute.
Mark Schultheiss
2010-08-20 12:49:29
@Mark Schultheiss: I knew that, assumed as OP says it is in **t_x** format but thanks anyways :)
Sarfraz
2010-08-20 12:51:50
Yes so your answer on the string I indicated gives "my" not "myt_endit" as the OP says "any text" (not beating on you, just giving an explaination for others as I see you know that :))
Mark Schultheiss
2010-08-20 12:59:12
A:
Try this:
// handle the click event of all elements whose id attribute begins with "t_"
$("[id^='t_']").click(function()
{
var id = $(this).attr("id");
// handle the click event here
});
Tim S. Van Haren
2010-08-20 12:30:28
half the answer, nothing to get the character string for the id attribute.
Mark Schultheiss
2010-08-20 13:04:06
A:
$("[id^='t_']").click(function()
{
var idEnding = $(this).attr("id");
idEnding.replace(/\t_/,'');
});
Using the click event capture the ID that begins with t_, then replace that with nothing giving a capture the end of the ID value.
Mark Schultheiss
2010-08-20 12:45:36