Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create button with createElement 1

Status
Not open for further replies.

gerases

Programmer
Apr 27, 2006
29
US
All right, a new problem.

I'm trying to create a button with with document.createElement but IE refuses to work right.

Here's the code:

basicElem = document.createElement("INPUT");
basicElem.setAttribute("type", "button");
basicElem.setAttribute("value", "edit");
basicElem.setAttribute("class", "buttonStyle1");
basicElem.setAttribute("onClick", "alert('ttt')");

The button is shown but neither "edit" works nor the style is honored.

I googled this and found out that one can create an element in IE this way:

basicElem = document.createElement("<input type='button' value='Edit' onClick=alert('ddd') class="buttonStyle1" />");

That DOES work but I would hate to resort to this "hack". Am I missing something in the original syntax? It works fine in Firefox, etc.
 
This is how you create a button with css.
As for setting an event using setAttribute save yourself the trouble trying since it's not supported anyway.
Code:
<html>
<head>

<script type="text/javascript">
<!--//
function createButton() {
	var target = document.getElementById("target");

	var button = document.createElement("INPUT");
	button.setAttribute("type", "button");
	button.setAttribute("value", "edit");

	// button.setAttribute("class", "button");     // Mozilla
	// button.setAttribute("className", "button"); // IE only

	button.id = "button"; // better solution

	target.appendChild(button);
}
//-->
</script>

<style type="text/css">
<!--
#button {
    border: 1px dashed #000000;
}
-->
</style>

</head>
<body onLoad="createButton()">

<span id="target"></span>

</body>
</html>

M. Brooks
 
Very nice. This is indeed working.

That's crazy that IE uses className... How about the "style" attribute? How do you set that in IE?

So, there's no cross-browser way of setting onclick when creating a button dynamically?
 
That's crazy that IE uses className... How about the "style" attribute?
How do you set that in IE?
Code:
button.style.width = "150px";
So, there's no cross-browser way of setting onclick when creating a button dynamically?
Not that I know of. Hopefully there will be a day that all browsers follow the same standards.

M. Brooks
 
Cool. Just tried it and it works perfectly both in IE and FF.
Thank you! Your comments were extremely helpful.
 
I don't know if this is cross-browser on not, but it's a little trick I found (reading the code for the DynArch JS calendar) for setting an event handler for on object.
Code:
basicElem["onclick"] = function() {alert('ttt')};
Note that "onclick" contains NO capital letters.

It works fine in IE.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Yep, you're absolutely right. That's what I ended up doing. I tried to play with attachEvent (IE only) and addEventListener (everything but IE) and onclick worked best.

Thank you!
 
You're welcome. I just ran across that and used it myself a couple of weeks ago, and you're the second person that asked a question where it came in useful since then.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top