Artemis909
Artemis909

Reputation: 149

Creating anchor tag from rel attribute

I am just about jQuery and attr rel usage.

I do not understand why this code is not working

$(".html5").append('<a target="_blank" href="'+ $(this).attr('rel') +'"></a>');

and following one is ok

$(".html5").append('<a target="_blank" href="'+ $(".html5").attr('rel') +'"></a>');

I just want to get rel atribute from html5 class and put this rel attribute to created anchor tag.There will be more classes with own rel like $(".html5, .css3, .js"), that is why I want to use $(this)

Upvotes: 0

Views: 255

Answers (1)

Tasos K.
Tasos K.

Reputation: 8079

You can use the .each(...) feature of jQuery where it loops all elements and runs some code for each item. For example:

$(".html5").each(function(index,item) {
    var jItem = $(item);
    jItem.append('<a target="_blank" href="' + jItem.attr('rel') + '"></a>');
});

Upvotes: 1

Related Questions