Gabe Dunn
Gabe Dunn

Reputation: 456

Change Text of Array Of links in jQuery

For my school's website they have a dropdown list of courses that you're currently enrolled in on the home page. However, if you see it, it is cluttered full of letters that many people might not want to see.

I already know how I'm going to do this. I'm going to use jQuery to select each list item:

var links = $(".d2l-datalist li .d2l-course-selector-item .d2l-left .vui-link");

This returns an array of <a> elements in text form.

Using links.text("Boo!"); I can set the text of all of them to "Boo!", but I want to change each one individually, using a for/in loop to itterate through each <a> and change the text depending on what the value of the href is.

However, whenever I do this, since the items in the array are strings, I cannot do anything to them with jQuery.

Any help with this is appreciated :)

Here's my code so far (running from a $.getScript() from a JS bookmarklet):

var links = $(".d2l-datalist li .d2l-course-selector-item .d2l-left .vui-link");
//links.text("Boo!");

var count = 1;
for (var link in links) {
    link.text("Boo #" + count);
    count += 1;
}

Relevant markup: http://hastebin.com/ulijefiqaz.scala

Upvotes: 0

Views: 189

Answers (1)

Rax Weber
Rax Weber

Reputation: 3780

You can use the jQuery .each iterator function.

var links = $(".d2l-datalist li .d2l-course-selector-item .d2l-left .vui-link");

var count = 1;

links.each(function() {
    $(this).text("Boo #" + count++);
});

Upvotes: 1

Related Questions