Is it possible to insert jQuery variable on an element's attribute?
15,614
Sure.
$(function() {
var url = 'http://www.google.com';
var data = '?one=1&two=2&three=3';
$('a:first').attr('href', url + data);
});
Related videos on Youtube
Author by
madzman23
Updated on June 04, 2022Comments
-
madzman23 almost 2 years
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. ;)
-
Matt Ball over 13 yearsapi.jquery.com Learn it. Love it. Live it.
-
-
madzman23 over 13 yearsThank 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..
-
Jacob Relkin over 13 years@madzman23, Please give me an example of what would go in the
href
attribute. -
madzman23 over 13 yearsAssuming 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.
-
We Are All Monica over 13 yearsAs far as I can tell, Jacob's answer is what you want.
-
madzman23 over 13 yearsYeah, 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!