How can I check selector to see if he is ending with number? JS code:
if(!$(this).val())
{
$('label[for^=image], input[id^=image], input[name=time]').remove();
}
I tried adding /d/, but it does not work (
$('label[for^=image' + /d/ +'], ....
)
How can I check selector to see if he is ending with number? JS code:
if(!$(this).val())
{
$('label[for^=image], input[id^=image], input[name=time]').remove();
}
I tried adding /d/, but it does not work (
$('label[for^=image' + /d/ +'], ....
)
Share Improve this question asked Jun 18, 2012 at 12:15 SashaSasha 8,72524 gold badges98 silver badges181 bronze badges 1-
So actually you want to test whether the value of the
for
attribute ends with a number? The whole string'label[for^=image], input[id^=image], input[name=time]'
is the selector... – Felix Kling Commented Jun 18, 2012 at 12:22
4 Answers
Reset to default 7demo is this what you are looking for: http://jsfiddle/8g2WL/
Behaviour: for the id's ending up with number in label
you will see an alert.
Hope this helps, please let me know if I missed anything.
sample code using regex
$('label').each(function(){
if ($(this).attr("id").match(/\d+$/) != null)
alert($(this).attr("id").match(/\d+$/));
});
html
<label id="foo">hulk</label><br/>
<label id="foo_23">HUlk with number</label><br/>
<label id="foo1">ironman with number </label><br/>
<label id="foo">hulk</label><br/>
you may need to use .filter()
to match with regex:
$('label').filter(function(){
var forValue = $(this).attr("for") || '';
return /^image\d+/.test(forValue);
});
$('label')
.filter(function() {
return $(this).attr("for").search(/image\d/)==0;
})
.html("Matched!")
;
Use extension Regex Selector for jQuery. Preview - http://jsfiddle/yDA9n/
$('label:regex(id,[0-9]$)').css('color', 'red');