Top Gun
Top Gun

Reputation: 688

Changing the attribute of the HTML tag using jQuery

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/");
});

Upvotes: 0

Views: 97

Answers (2)

Nico
Nico

Reputation: 7256

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

fiddle

Upvotes: 2

lyndact
lyndact

Reputation: 1

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>

Upvotes: 2

Related Questions