473,412 Members | 2,069 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,412 software developers and data experts.

javascript ignored?

PJ6
I'm new to this, so please bear with me if this is a dumb question -

In OnClick, this code is ignored -

this.class='someclass'

This is also ignored -

document.getElementByID('someid').class = 'someclass'

Both are ignored, but neither produce an error.

This code does work -

setclass('someid','someclass')

where this function is included on the page -

function setclass(id, classname)
{
var elem = document.getElementById(id);
elem.className = classname;
}

I don't understand why the first two don't work but the last one does.

Also, if I have a fucntion like

function settext(id, text)
{
var elem = document.getElementById(id);
elem.text='test';
}

and try to apply it to any element with text as a property, the text isn't
changed at all.

Can anyone clue me in?

Thanks,
Paul
Oct 15 '05 #1
5 1650
PJ6 said the following on 10/15/2005 4:37 PM:
I'm new to this, so please bear with me if this is a dumb question -

In OnClick, this code is ignored -

this.class='someclass'

This is also ignored -

document.getElementByID('someid').class = 'someclass'

Both are ignored, but neither produce an error.

This code does work -

setclass('someid','someclass')

where this function is included on the page -

function setclass(id, classname)
{
var elem = document.getElementById(id);
elem.className = classname;
}

I don't understand why the first two don't work but the last one does.
Because the first two try to change the .class property while the latter
changes the .className property. The difference is extremely significant.
Also, if I have a fucntion like

function settext(id, text)
{
var elem = document.getElementById(id);
elem.text='test';
}

and try to apply it to any element with text as a property, the text isn't
changed at all.


What types of "any element" that has a text property are you trying to
change?

Also, it depends on the HTML and how the function is called.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 15 '05 #2
PJ6
"Randy Webb" <Hi************@aol.com> wrote in message
news:5o********************@comcast.com...
What types of "any element" that has a text property are you trying to
change?


ClassName.. thanks for pointing that out, I should have seen that. Maybe I
need some sleep.

Let's pick the one I'm working on at the moment. Is it possible to change
the text in a <SPAN> element client-side?

Paul
Oct 15 '05 #3
PJ6 wrote:
ClassName.. thanks for pointing that out, I should have seen that. Maybe I
need some sleep.
className , actually

Let's pick the one I'm working on at the moment. Is it possible to change
the text in a <SPAN> element client-side?


Yes, indeed it is. element.innerHTML="new text"
Mick
Oct 16 '05 #4
PJ6 wrote:
"Randy Webb" <Hi************@aol.com> wrote in message
news:5o********************@comcast.com...
What types of "any element" that has a text property are you trying to
change?

ClassName.. thanks for pointing that out, I should have seen that. Maybe I
need some sleep.

Let's pick the one I'm working on at the moment. Is it possible to change
the text in a <SPAN> element client-side?


As suggested by Mick, innerHTML is likely best if you want to completely
replace the contents of the span and what you are putting in there is
either plain text or text with some minimal markup (say text with a few
<em> or <b> elements).

For completeness, you should know about the DOM methods too. If you
know that the span has one text node, and that is what you want to
replace, then:

theSpan.firstChild.data = 'some text';

will do the trick. If there are multiple text nodes, or other elements
to replace also, there's a bit more to it.

If you want to add a complex document fragment, using the document
object model (DOM) may be a better solution, even though it requires
more code. To remove the content of the element, you can use the quick
'n dirty:

theSpan.innerHTML = '';

or DOM:

while (theSpan.firstChild) {
theSpan.removeChild(theSpan.firstChild);
}

Now to add the text:

theSpan.appendChild(document.createTextNode('some new text'));

If you want to add part of a table (you wouldn't do that inside a span
element but you may want to use it elsewhere), then DOM is the only way
to go - innerHTML will likely fail in most browsers, including IE.

innerHTML has some other foibles, so use with caution for complex
situations and consider using DOM instead.

Here's some play code:

<p>The span follows: <span id="spanA">text to replace</span></p>

<input type="button" value="Replace text DOM" onclick="
var theSpan = document.getElementById('spanA');
theSpan.firstChild.data = 'DOM replacement text';
">
<br>

<input type="button" value="Replace text innerHTML" onclick="
var theSpan = document.getElementById('spanA');
theSpan.innerHTML = 'innerHTML replacement text';
">
<br>

<input type="button" value="Long DOM method" onclick="
var theSpan = document.getElementById('spanA');
while (theSpan.firstChild) {
theSpan.removeChild(theSpan.firstChild);
}
var txt = 'Long DOM replacement text'
theSpan.appendChild(document.createTextNode(txt));
">


--
Rob
Oct 16 '05 #5
PJ6
Thanks for the help. :)

Paul
Oct 17 '05 #6

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

Similar topics

1
by: 4levels | last post by:
Dear Folks, I stumbled upon a strange behaviour of the XMLHttpRequest.. Maybe I'm just not well informed enough about its possibilities, so could someone please confirm my question? When I...
16
by: howachen | last post by:
e.g. <script type="text/javascript" src="js/common.js"></script> but not <script type="text/javascript" src="js/common.js" /> for any reason?
9
by: Erwin Moller | last post by:
Hi, Can anybody comment on this? In comp.lang.php I advised somebody to skip using: <script language="javascript"> and use: <script type="text/javascript"> And mr. Dunlop gave this response:
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.