<html>
<head></head>
<body>
<span class="mtb-price">
<label Class="mtb-ofr"><b class="lb1"></b>AAAAA</label></span>
<script>
var sku = document.getElementsByClassName("mtb-ofr").childNodes[1].nodeValue;
alert(sku);
</script>
</body>
</html>
How do i access childNode of having class name 'mtb-ofr' using document.getElementsByClassName() ? and what should be the alternative of document.getElementsByClassName() to obtain the same result ??
<html>
<head></head>
<body>
<span class="mtb-price">
<label Class="mtb-ofr"><b class="lb1"></b>AAAAA</label></span>
<script>
var sku = document.getElementsByClassName("mtb-ofr").childNodes[1].nodeValue;
alert(sku);
</script>
</body>
</html>
How do i access childNode of having class name 'mtb-ofr' using document.getElementsByClassName() ? and what should be the alternative of document.getElementsByClassName() to obtain the same result ??
Share Improve this question asked Jun 22, 2012 at 18:41 Pooja DesaiPooja Desai 2271 gold badge6 silver badges16 bronze badges 3-
2
document.getElementsByClassName("mtb-ofr")[0].childNodes
? or just.children
. – sachleen Commented Jun 22, 2012 at 18:45 - 2 getElementsByClassName returns an array, you need to select an item of the array before you can drill down any deeper – jackwanders Commented Jun 22, 2012 at 18:45
- Basically what i want is to replace AAAAA with some other text. – Pooja Desai Commented Jun 22, 2012 at 18:48
3 Answers
Reset to default 2to change the label's text:
document.getElementsByClassName("mtb-ofr")[0].childNodes[1].nodeValue = 'something';
getElementsByClassName
returns an array. You need to first get the element from the NodeList.
document.getElementsByClassName("mtb-ofr")[0].childNodes[1].nodeValue
Use jQuery !
$('label.mtb-ofr').children()
You're done !
And you can filter more by passing parameters to children().
http://api.jquery./children/