473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

set dom element class (if element was dynamically created)

hi everybody, didn't find this using the search :( this is my problem:
i create a dom element dynamically (<span>) and want to assign a class
attribute to it such that it has some css style, this works in ie, but
not in firefox :( here's the simple code:

<div id="somediv">
</div>

var div = window.document.getElementById("somediv");
var span = window.document.createElement("span");
span.innerHTML = "span";
span.attributes.getNamedItem("class").nodeValue = "span_class";
div.appendChild(span);

so this works well in ie6, but firefox gives this error:
Error: span.attributes.getNamedItem("class") has no properties

if i run this code on a statically defined span (in the html file),
where a class attribute already is set:
<div id="somediv">
<span id="spanspan" class="">
</span>
</div>
changing the class to "span_class" works both in ie and firefox.

how can i fix my above code such that it works in both browsers? it
seems that on newly created dom elements the class attribute isn't even
there (to change it), how can i create it, and then change it?

thanks for your time, all the best

Jan 25 '06 #1
3 9488
span.className = "span_class";

Jan 25 '06 #2
thanks marss, this solved the problem!

i also found another (a bit longer) solution as an alternative, if it
helps somebody :)

var class_attrib = window.document.createAttribute("class");
class_attrib.nodeValue = "span_class";
span.attributes.setNamedItem(class_attrib);

Jan 25 '06 #3
ted benedict wrote:
thanks marss, this solved the problem!

i also found another (a bit longer) solution as an alternative, if it
helps somebody :)

var class_attrib = window.document.createAttribute("class");
class_attrib.nodeValue = "span_class";
span.attributes.setNamedItem(class_attrib);


'class' is a future reserved word in JavaScript 1.5 (the version most
commonly implemented in modern browsers), hence the use of 'className'
to refer to the HTML element attribute 'class'.

There are proposals to add classes (and hence use the 'class' keyword)
to ECMAScript Ed 4:
<URL:http://www.mozilla.org/js/language/es4/core/classes.html>

and JavaScript 2.0:
<URL:http://www.mozilla.org/js/language/js20/core/classes.html>.

Microsoft has already added classes to JScript .NET.
<URL:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting07142000.asp>


--
Rob
Jan 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
6
by: bradb | last post by:
Hello, I have a textbox with an empty span element (place holder) next to it. When the user adds some text to the text box, I create a checkbox in the empty span element. When I create the...
4
by: assgar | last post by:
Hi I am stuck on a problem. I use 3 scripts(form, function and process). Development on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. The form displays...
1
by: vega80 | last post by:
Hi. I have a problem with assigning an onkeypress-function to dynamically created input-boxes.I want to put the content of an input-field into a tag-list when the user hits enter. This works...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
2
by: gubbachchi | last post by:
Hi, I have a form whose elements are created dynamically on selection, i.e. the form has only text boxes and the number of text boxes depends on the users selection, if user selects 3 then 3...
1
by: spyderfusion02 | last post by:
Hi there, I am trying to get the class names dynamically for the script below. I am using the Pagination plugin. I have different class names that are created through PHP so need to get them using...
4
Claus Mygind
by: Claus Mygind | last post by:
I understand how to link an element that exists on the form with a prototype function or class. But how do I link/bind a dynamically created element with the same prototype function or class In...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.