0

I am trying to change the attribute of an <a> tag using jQuery but it's not working. I am trying to change the src attribute of the <a> tag from www.google.com to www.somewebsite.com. Can anyone please tell me where I am getting it wrong.

<p class="form-row text-varient">
    <label class="checkbox">
        Some text here
        <a target="_blank" href="http://google.com">Link Text</a>
    </label>
</p>
jQuery(document).ready(function(){
    jQuery(".text-varient").find("a").attr("href", "http://www.somewebsite.com/");
});
6
  • Where does the .terms come from? It's not visible in your provided HTML Commented Nov 22, 2015 at 17:22
  • Where is the .terms element? The only reason this code wouldn't work is if that element is not in the source. Commented Nov 22, 2015 at 17:22
  • Do you have an element with class terms? Commented Nov 22, 2015 at 17:24
  • Its not terms its .text-varient Commented Nov 22, 2015 at 17:25
  • Well, given your update, the code works fine: jsfiddle.net/qhr1edkf. If you're still having issues, check the console for errors. Commented Nov 22, 2015 at 17:28

2 Answers 2

2
jQuery(document).ready(function(){
    $(".checkbox").find('a').attr("href", 'http://www.somewebsite.com');
 });

fiddle

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

Comments

2

Try this:

<p class="form-row text-varient">
<label class="checkbox">
    Some text here
    <a target="_blank" href="http://google.com">Link Text</a>
</label>
</p>

<script>

$(document).ready(function(){

    $(".checkbox a").attr("href", "http://www.somewebsite.com");

});



</script>

1 Comment

Thanks for your reply but this will change all the attributes of the <a> tag in the website i want this specific attribute to be changed

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.