• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

dynamically create href link

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this out:

 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you actually looked at what you were ouptputting it would be

<a href="blah.html?name="asdf">

Now do you see whay you never get the value? Now what if a user enters in a " character? Say issue will come up. You need to escape() the text.

Eric
 
reply
    Bookmark Topic Watch Topic
  • New Topic