I am trying to obtain the value of label elements using jQuery (or javascript). The only search term I have is a substring of the label element’s for attribute. In the example code shown below, this would be ‘Okuku’.
<label class="required" for="Nigeria-Osun-Okuku">The Township of Okuku</label>
The Script:
$("label[for='Nigeria-Osun-Okuku']").text();
would return
The Township of Okuku
but I don’t have the complete ‘for’ value: Nigeria-Osun-Okuku but the last substring of it: ‘Okuku’.
How can I script to search, match and return: The Township of Okuku ? Thanks
You could use the
attribute-containsselector:Note, this will grab all elements that match that substring.
Demo: http://jsfiddle.net/sYdN4/
If you are sure that this substring is found at the end of the value, you could use the ends-with selector:
Which will return elements whose
forattribute ends with the substring.