
Originally Posted by
elduderino
HI,
Code JavaScript:
var titlea = document.createElement('a');
titlea.id = 'list_' + count;
titlea.href = '/results.php?' + articleid;
var txt = document.createTextNode('select....');
titlea.appendChild(txt);
con.appendChild(titlea);
I have this code. I've created an a tag with with a href and id attribute. I also need to add some javascript
:
onclick="replaceParent();"
I've googled for a solution but can't find an answer....how do i do this?
The best way to do this would be to assign the link an id or a class.
Code:
function do_onclick()
{
var obj = document.getElementByID('IDNAME');
obj.onclick = function()
{
replaceParent();
}
}
window.onload = function()
{
do_onclick();
}
With classes its a little be different by not that much.
Code:
function do_onclick()
{
var anchors = document.getElementsByTagName('a');
for(var i=0; i<anchors.lenght; i++)
{
var obj = anchors[i];
if(obj.className == 'CLASSNAME')
obj.onclick = function()
{
replaceParent();
}
}
}
window.onload = function()
{
do_onclick();
}
That's an example of how you would do it. But I don't know if it will work or not so don't take my word for it. I'm in Las Vegas flying to CT and I haven't slept for 24 hours
. I may have a few errors here or there. I hop eyou get the general idea though
.
Bookmarks