473,767 Members | 6,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='som eclass'

This is also ignored -

document.getEle mentByID('somei d').class = 'someclass'

Both are ignored, but neither produce an error.

This code does work -

setclass('somei d','someclass')

where this function is included on the page -

function setclass(id, classname)
{
var elem = document.getEle mentById(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.getEle mentById(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 1669
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='som eclass'

This is also ignored -

document.getEle mentByID('somei d').class = 'someclass'

Both are ignored, but neither produce an error.

This code does work -

setclass('somei d','someclass')

where this function is included on the page -

function setclass(id, classname)
{
var elem = document.getEle mentById(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.getEle mentById(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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 15 '05 #2
PJ6
"Randy Webb" <Hi************ @aol.com> wrote in message
news:5o******** ************@co mcast.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.innerHT ML="new text"
Mick
Oct 16 '05 #4
PJ6 wrote:
"Randy Webb" <Hi************ @aol.com> wrote in message
news:5o******** ************@co mcast.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.firstCh ild.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.innerHT ML = '';

or DOM:

while (theSpan.firstC hild) {
theSpan.removeC hild(theSpan.fi rstChild);
}

Now to add the text:

theSpan.appendC hild(document.c reateTextNode(' 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">tex t to replace</span></p>

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

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

<input type="button" value="Long DOM method" onclick="
var theSpan = document.getEle mentById('spanA ');
while (theSpan.firstC hild) {
theSpan.removeC hild(theSpan.fi rstChild);
}
var txt = 'Long DOM replacement text'
theSpan.appendC hild(document.c reateTextNode(t xt));
">


--
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
2192
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 put plain javscript in a file that is read-in through a XMLHttpRequest-object, it's like it is totally ignored. Eg. I have the file ajax_include.html with in it's body the following lines <script type="text/javascript" language="javascript">
16
1875
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
8426
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
9575
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9407
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9960
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9841
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.