Wrox Programmer Forums
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 13th, 2004, 08:03 AM
Registered User
 
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default setAttribute()

I've been trying to get this piece of code working with a button, but there seems to be a problem:


setAttribute(document.getElementById("but").setAtt ribute("onclick","alert('hello')";

<input type=button id="but" onclick="alert("ello")">


It seems to me that i am unable to change the onclick attribute of the button. Can someone help me?



Marcus
 
Old January 13th, 2004, 04:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your code doesn't look quite right -- why are you calling setAtribute() twice? Why are you using the return value from your inner setAttribute() call as the first parameter to your outer setAttribute() call? Also -- your parens aren't matching up either. Finally, your HTML button is also not syntactically correct. You use double-quotes to encapsulate your onClick attribute value, but you also use double-quotes within that string for the parameter to alert().

The first double-quote will end the string, and the rest of the stuff is garbage to the HTML parser. More specifically:

  onclick="alert("

is your attribute value, and

 ello")"

is garbage.


Setting attributes is usually browser-dependant, especially for built-in attributes. It's possible that some browsers expect you to set "onClick", and others expect "onclick", etc... Slight variations in spelling might affect the outcome.

For IE, I find that it's easier just to rewrite the innerHTML attribute:

document.getElementById("but").innerHTML = "<input type=\"button\" id=\"but\" onClick=\"alert('Hello')\">";



I strongly suggest searching the net for other tutorials and examples:
  http://www.google.com/search?q=javas...ribute+onClick


Take care,

Nik
http://www.bigaction.org/
 
Old January 14th, 2004, 09:16 AM
Registered User
 
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Whoops... i've typed in my code wrongly:


var newElem = document.createElement("INPUT");
newElem.setAttribute("type","button");
newElem.setAttribute("onClick","alert('Hello')");

document.appendChild(newElem);


I'm trying to create a new button on the page with Javascript. Assuming that my new node is displayed on the document, i'm unable to set the "onClick" event for the button. I think that its either that setAttribute() can't do it, or that i have to use the attachEvent() thingy... Can someone help me?!



Thanx

 
Old January 11th, 2005, 09:24 AM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ppamo
Default

[quote]Originally posted by ongmarcus12

var newElem = document.createElement("INPUT");
newElem.setAttribute("type","button");
newElem.setAttribute("onClick","alert('Hello')");

document.appendChild(newElem);


Hi...
I thing you want to do this....

var newElem = document.createElement("INPUT");
newElem.setAttribute("type","button");
newElem.attachEvent("onClick",element_onClick);

document.appendChild(newElem);
.
.
.

function element_onClick()
{
   alert ('hello');
}


...
bye
 
Old July 11th, 2006, 10:24 AM
Registered User
 
Join Date: Jul 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i realize this is an old post, but the correct solution has not been posted... i'm offering this to help all those who have struggled with this issue as i have. to my knowledge, this is the only method that works in IE and firefox:

var newElem = document.createElement("INPUT");
newElem.setAttribute("type","button");
newElem.onclick = function () {element_onClick()};

document.appendChild(newElem);
.
.
.

function element_onClick()
{
   alert ('hello');
}

www.informationarchitech.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
add id tag with setAttribute generates escape char neilstairmand Javascript 1 June 6th, 2007 05:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.