Reputation: 341
currently I have a textbox in which someone can type in a link, and then a link on the page next to the textbox should change it's href attribute to the text the user just typed in.
My javascript:
var LinkText = $("[id$=TextBox]").val();
$("[id$=DocumentLink]").href = LinkText;
My HTML:
<a id ="DocumentLink" target = "_blank" href="http://www.currentlink.com/">Link to Document</a>
<input id="TextBox" type="text" /> `
Although LinkText is picked up as the string typed in the textbox, the second line of my javascript is not working as I want. The link stays as the currentlink. I have jQuery 1.4.2 if that helps, I could be doing something that doesn't work with that maybe.
Upvotes: 0
Views: 97
Reputation: 1
set the attr of the link.
$("[id$=DocumentLink]").attr("href", LinkText);
i think it would be much easier to use JS and innerHTML
.
Upvotes: 0
Reputation: 144689
jQuery object doesn't have href
property. You are defining a property for the jQuery object which doesn't affect the href
property of the HTMLElement object in the jQuery collection. You could use the attr
or the prop
method instead.
Upvotes: 1
Reputation: 104775
Since you have a jQuery object you have to set the attr
$("[id$=DocumentLink]").attr("href", LinkText);
Or you can get the actual HTMLElement at the 0
index and call .href
that way:
$("[id$=DocumentLink]")[0].href = LinkText;
And since your matching an exact ID, just use $("#DocumentLink")
Upvotes: 2