Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  93] [ 6]  / answers: 1 / hits: 63074  / 11 Years ago, sun, september 22, 2013, 12:00:00

I am writing some javascript to be run in a bookmarklet that should get the text within a element that has a certain class name.



So for example



document.getElementsByClassName('price')


where the web page has or example



<span class=price>

£23

</span>


How would I go about getting the actual text within the the element that has a class name price (i.e £23 in the above example).


More From » javascript

 Answers
37

getElementsByClassName returns an array of elements. Traverse through that array and get the innerText property of each. For example:



var priceEls = document.getElementsByClassName(price);
for (var i = 0; i < priceEls.length; i++) {
var price = priceEls[i].innerText;
alert(Price: + price);
}


Live demo: http://jsfiddle.net/YQsBW/


[#75515] Saturday, September 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 2 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;