0

Based on the below link as an example:

...if a link tag contains the class "add-selected", I want jquery to add an href called "#scrollTo" to all corresponding 's.

<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>

Final result after page loads should be:

<a class="add-selected btn btn-default" href="#scroll">
    <span class=" ion-plus-circled"></span>Add Selected
</a>
  • 3
    Okay, and where is your attempt first? StackOverflow isn't a coding service, we're here to help, not to do it all for you. – George Jun 16 '17 at 14:18
  • It's a bit unclear if you want `#scrollTo` or `#scroll`. (Different in question and in HTML). – Arg0n Jun 16 '17 at 14:20
  • Searching for `add href based on class` ► https://stackoverflow.com/questions/30981092/dynamically-add-href-based-on-class-name-of-parent-list-item - While that answer shows parent elements it is shown there how to replace a `href` value based on a class selector. - Try that and if you have issues you can post your code with the problem. – Nope Jun 16 '17 at 14:22

2 Answers2

2

Try this :

$(function(){
   $('a.add-selected').attr('href','#scrollTo');
});
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
2

$('.add-selected').attr('href', '#scroll');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>
<br/>
<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>
<br/>
<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>
<br/>
<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>
Arg0n
  • 8,283
  • 2
  • 21
  • 38