Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector : get the content of 1st element class containing specific word

Quick jQuery selector question :

I need to get the content of the first element (div/span/...) having a class containing a string (eg : "price").

Something not like this ;o) :

var price = ( $('[class*=price]').text() );

Thanks a lot !!!

like image 477
user1045587 Avatar asked Dec 21 '11 15:12

user1045587


People also ask

Which selector selects all elements that contain specified text in jQuery?

The :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element.

How do I target a specific element in jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

How do you select the first element with the selector statement?

The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the first element in a group (like in the example above).


1 Answers

This should work fine using the first selector:

$('[class*="price"]:first').text()

http://jsfiddle.net/Et3vx/1

like image 83
Richard Dalton Avatar answered Oct 09 '22 22:10

Richard Dalton