I am using $('label:contains('something');
to get the label that contains ‘something’ in it
it works perfectly fine but I want to be able to handle more than one label how to do it
example:
HTML:
<html>
<label for='michel'>My Name is Michel</label>
<label for='allen'>My Name is Allen</label>
<label for='chris'>His Name is Chris</label>
</html>
jQuery:
<script>
label = $('label:contains(My Name is)');
console.log(label.attr('for'));
</script>
now the console logs only michel
I want to be able to handle more than one value, I want to log michel and allen
2
Answers
You could use jQuerys each function to iterate over all the labels.
$('label:contains(My Name is)');
return the selected document object that matches the give parameters. In your case there are two elements are being selectedTo access the each of the item from the selected object you need to loop through it. See the following example.