views:

33

answers:

2

Hi, I got just want to ask if its possible to insert a jQuery variable on an attribute. Here is a 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 to the href. How can I do that? Thank you in advance. ;)

+3  A: 

Sure.

$(function() {

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

   $('a:first').attr('href', url + data);
});
Jacob Relkin
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
@madzman23, Please give me an example of what would go in the `href` attribute.
Jacob Relkin
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.
madzman23
As far as I can tell, Jacob's answer is what you want.
jnylen
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!
madzman23
A: 

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);
});
jnylen