2

I would just like to to ask if its possible to insert a jQuery variable on an attribute. Here is my sample code:

<html>
<head>
<script type="text/javascript">
      $(function() {

   var url = 'http://www.google.com';
   var data = '?one=1&two=2&three=3';

});

</script>
</head>
<body>    
      <a href="jquery var">Click here</a>
</body>
</html>

I need to put the jQuery var value of the href. How can I do that? Thank you in advance. ;)

1
  • 1
    api.jquery.com Learn it. Love it. Live it. Commented Oct 13, 2010 at 3:26

2 Answers 2

7

Sure.

$(function() {

   var url = 'http://www.google.com';
   var data = '?one=1&two=2&three=3';

   $('a:first').attr('href', url + data);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Jacob, I know how to assign a variable on the anchor attribute but I want to hardcode the exact variable on the <a href="jquery var should be put here"></a>. Do you think it is possible? It should be something like in PHP, where you can changed the values using $_POST..
@madzman23, Please give me an example of what would go in the href attribute.
Assuming we declared the value of the var. here is what I want to put on "HTML" code: <a href="url+data">Click Here!</a> i want to put the hardcoded value of the href because I need to open the href using thickbox modal and I need to pass value on the page I call. Of course it works with your solution .attr() but I need to complete the href for the thickbox modal. I really dont know if its possible and how can I do it.
As far as I can tell, Jacob's answer is what you want.
Yeah, but it doesn't give the output I needed. Anyways, thank you for your answers, I already fix the problem using a server script and assigned it to the href. I think jquery is not capable of doing that because serverside and clientside is on different realm. ;) regards!
0

If you want to associate more complex objects with an element:

$(function() {
    // Store data...
    $('a:first').data('jQuery', { a: 1, b: 2 });

    // Then retrieve it later...
    alert($('a:first').data('jQuery').a);
});

Comments

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.