AKIWEB
AKIWEB

Reputation: 19612

passing variable value to href argument in anchor tag

how to pass the variable value to href argument in anchor tag.

<body>
<script>
var id = "10";
$('a_tag_id').attr('href','http://www.google.com&jobid='+id);
</script>


<a id="a_tag_id">something_here</a>
</body>

I want anchor tag to look like this after the above code is executed.

<a href="http://www.google.com&jobid=10">something_here</a>

But somehow the above code is not working. Is there anything wrong I am doing?

Upvotes: 1

Views: 28291

Answers (6)

Abhitalks
Abhitalks

Reputation: 28387

$('#a_tag_id').attr('href','http://www.google.com?jobid='+id);

The selector should be for id '#', and the querystring starts with "?".

BTW: Isn't this question following up on your last question: Pass an id to anchor tag ?

Upvotes: 0

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

You have miss # in jQuery selector, and insert the code inside the document.ready to make that your script work when the page is ready

Try this:

<script>
$(document).ready(function(){
   var id = "10";
   $('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
});
</script>

DEMO

Upvotes: 3

MACMAN
MACMAN

Reputation: 1971

Modify the jquery like this

 $('#a_tag_id').attr('href','http://www.google.com&jobid='+id);

Upvotes: 1

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Use # for id selctor

$('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
   ^

Fiddle Demo

Upvotes: 1

hungerstar
hungerstar

Reputation: 21685

You're missing the # for the id. Try using $('#a_tag_id'). Use an ? before your first query string variable instead of &.

Upvotes: 1

Uooo
Uooo

Reputation: 6334

When your Javascript is executed, the a tag might not exist yet.

Use:

$(document).ready(function(){
    var id = "10";
    $('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
});

Also, it should be #a_tag_id to match the id.

Upvotes: 0

Related Questions