Prasanth K C
Prasanth K C

Reputation: 7332

How to append data to elements attribute value

Having htmls like this,

<a class="testDiv" href="/link1"></a>
<a class="testDiv" href="/link2"></a>

I'm trying to append ?param1=2 to the href attributes of all <a> tags.

So that it should look like,

<a class="testDiv" href="/link1?param1=2"></a>
<a class="testDiv" href="/link2?param1=2"></a>

Something like this,

$(".testDiv").attr('href').append("?param1=2");

Is there any possible way for this?

Upvotes: 1

Views: 45

Answers (1)

j08691
j08691

Reputation: 208032

$(".testDiv").attr('href', function (_, currentHref) {
    return currentHref +"?param1=2"
});

Upvotes: 4

Related Questions