bobek
bobek

Reputation: 8020

javascript grabbing span tag with both class and id

I have a span tag

<span class="vi-is1-prcp" id="v4-25">US $99.00</span>

I would like to grab it using pure javascript. JQuery or any other library is not allowed. Is that possible?

I recon that

getElementById('v4-25')

won't work since I have to specify class, too, correct?

Thank you,

So,

<div id="listprice">asdasdasdasdasd</div>
var string = document.getElementById('v4-25');
document.getElementById('listprice').innerHTML = string;

should print value of 'v4-25' in 'listpirce' ?

H

Upvotes: 1

Views: 297

Answers (3)

Joseph Marikle
Joseph Marikle

Reputation: 78590

First of all, ids are unique. You can't have more than one. therefore, when you select element by id, you can only bring back one element (this is good).

Secondly, after you get an element, you have to do something with it. var string = document.getElementById('v4-25'); only gets you the element, but it looks like you want var string = document.getElementById('v4-25').innerHTML; for the price. If you do want the id instead you can do var string = document.getElementById('v4-25').id; but because that just returns "v4-25" it's a bit redundant.

Upvotes: 0

Phillip Burch
Phillip Burch

Reputation: 459

There is no reason to add a class. Run the script after that dom element is loaded like this.

<span class="vi-is1-prcp" id="v4-25">US $99.00</span>
<script>
    var elm = document.getElementById('v4-25');
</script>

Upvotes: 0

Jacob
Jacob

Reputation: 78920

getElementById will work just fine. Just make sure you're running it after the page has loaded.

Upvotes: 3

Related Questions