Setting a link URL for a div

D

DB

I'm just getting into DHTML, and there's one thing I'm getting stuck on
in my current project. I'm building a list of destinations to display in
a window. Each one is in its own separate div. But I can't seem to
figure out how to add a link to those div's on the fly in my script. Is
there a way to do that in JavaScript?
 
R

Randy Webb

DB said the following on 10/30/2006 3:23 PM:
I'm just getting into DHTML, and there's one thing I'm getting stuck on
in my current project. I'm building a list of destinations to display in
a window. Each one is in its own separate div. But I can't seem to
figure out how to add a link to those div's on the fly in my script. Is
there a way to do that in JavaScript?

Yes. And how you would do it depends on the HTML you use. Why is each
link in it's own div though? That's overkill if all it is for is to hold
a link.
 
A

ASM

DB a écrit :
I'm just getting into DHTML, and there's one thing I'm getting stuck on
in my current project. I'm building a list of destinations to display in
a window. Each one is in its own separate div. But I can't seem to
figure out how to add a link to those div's on the fly in my script. Is
there a way to do that in JavaScript?

function addLink(divId,link) {
// search, get text inside the div of id div
var txt = document.getElementById(divId);
while(txt.nodeType != 3) txt = txt.firstChild;
// new link tag
var L = document.createElement('A');
// new link href
L.href = link;
// insertion text to new link (precedent text catched)
L.innerHTML = txt.nodeValue;
// place of new link in document body
var target = txt.parentNode;
// delete old text
target.innerHTML = ''
// insertion
target.appendChild(L);
}

</script>
<div id="e1"><p>essai 1</p></div>
<div id="e2"><p>essai 2</p></div>
<div id="e3"><p>essai 3</p></div>
<p><a href="#"
onclick="addLink('e2','page2.htm');return false;">
page 2</a></p>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top