/    Sign up×
Community /Pin to ProfileBookmark

dynamically create href link

I have a <span id=”aa”> to enclose a link tag with href=”#” as the url. When a user clicks some button, I want to put some real href address to it. So I use:

var na = document.getElementById(“nameField”).value;

document.getElementById(“aa”).innerHTML = “<a href=”blah.html?name=””+na+”” >Products</a>”;

to replace previous value within the span.

But when I move mouse over the link, it will only show ‘blah.html?name=’, the value of name is missing. I got the variable na’s value correctly by using document.getElementById()… Why can’t the link show the value of na?

Someone please help, thanks.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@carentricaAug 30.2006 — I think what you need is:-

document.getElementById("aa").innerHTML = '<a href="blah.html?name='+na+'">Products</a>';
Copy linkTweet thisAlerts:
@KorAug 30.2006 — DOM is the standard way:
<i>
</i>document.getElementById('aa').getElementsByTagName('a')[0].href='blah.html?name='+na;
Copy linkTweet thisAlerts:
@scorpioyauthorAug 30.2006 — Thanks. It works now by using

document.getElementById('aa').getElementsByTagName('a')[0].href='blah.html?name='+na;

But my link tag also has onclick="return false;", when I change it to onclick="return false;" by using:

document.getElementById('aa').getElementsByTagName('a')[0].coclick='return true';

It doesn't work on Firefox.

Is there anything wrong with this onclick attribute?
Copy linkTweet thisAlerts:
@KorAug 30.2006 — <i>
</i>document.getElementById('aa').getElementsByTagName('a')[0].onclick=function(){return true}
×

Success!

Help @scorpioy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...