11

Example HTML:

<div id="video-quality" style="">
<a class="low bttn" href="">
<span>Low Quality</span>
</a>
</div>

Hey im trying to use JavaScript to target an anchor tag and change it's current text of "Low Quality" to "High Quality".

I don't really know JavaScript very well, but this is what I have come up with. But it's obviously not working:

var vid_id=document.getElementById('video-quality');
var anchor=vid_id.getElementsByTagName('a');
anchor.innerHTML="High Quality";
5
  • 1
    The result of .getElementsByTagName() is an array. You should access it's elements and not the array itself. Commented Nov 7, 2012 at 23:33
  • 2
    @MarkoDumic: It's not an array. It's a NodeList. Commented Nov 7, 2012 at 23:34
  • @T.J. Crowder said that the @MarkoDumic was a Array for clarity for novice users how to work. Commented Nov 7, 2012 at 23:36
  • @T.J.Crowder: Of course, but I just wanted to point out rather then be precise, otherwise I'd need to explain what a NodeList is (the OP is obviously not proficient with JS). Commented Nov 7, 2012 at 23:36
  • @MarkoDumic: Fair 'nuff, it's just that there are substantial differences between them (not least that NodeList instances are live). Perhaps "...is an array-like thing called a NodeList" with a link to the spec. Commented Nov 7, 2012 at 23:38

4 Answers 4

7

Assuming the video-quality div will only ever have one anchor in it, you're not far off:

var vid_id=document.getElementById('video-quality');
var anchor=vid_id.getElementsByTagName('a');
anchor[0].innerHTML="High Quality";
//    ^^^-- change here

(There's a shorter way if you don't need to support IE7 and earlier, see David's answer for more.)

The key was in the name getElementsByTagName (note the plural). It returns a NodeList, not an element. A NodeList is (shockingly!) a list of matching nodes, in this case matching elements. innerHTML is a property of individual elements, so we have to index into the NodeList to access a specific element. The code above assumes there is at least one matching element, and updates its HTML. If you want to be more defensive:

var vid_id=document.getElementById('video-quality');
var anchors=vid_id.getElementsByTagName('a');
if (anchors[0]) {
    anchors[0].innerHTML="High Quality";
}

That allows for the possibility there are no matching anchors.

Note that by doing this, you're removing the span element, since the span is a descendant of the anchor. If you wanted to avoid that, just look for span elements as descendants of the anchor, exactly as you've looked for anchors in the div.


FWIW, I'd recommend using a reasonable JavaScript browser-focussed library like jQuery, YUI, Closure, or any of several others. They provide a lot of utility functionality, and smooth over some browser differences. But if you prefer using the DOM directly, that's fine too.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that this will also remove the SPAN element around the text node.
5

If you don’t need legacy support (IE7-), you can write:

document.querySelector('#video-quality a span').innerHTML = 'High Quality';

If you DO need legacy support, I recommend a library like jQuery or similar to make selecting DOM nodes much simpler from a singe, wide-spread API.

Comments

5

Plain javascript:

var vid_id = document.getElementById('video-quality');
var span = vid_id.getElementsByTagName('span');
span[0].innerText='High Quality'

Or you can just use jQuery:

$('#video-quality span').text('High Quality');

hope this helps.

1 Comment

A) The OP hasn't mentioned jQuery. B) Your "plain JavaScript" example doesn't work for the same reason the OP's code doesn't work.
3

Simple is best. Just give the span element an id and then alter it directly.

<div id="video-quality" style="">
<a class="low bttn" href="">
<span id="quality-setting">Low Quality</span>
</a>
</div>

then script is:

document.getElementById('quality-setting').innerHTML="High Quality";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.