473,890 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very simple question from a novice

I try to change the text in a <p> using getElementById( ). I wonder what properties exists, and which one to use here. (The following does not work.)

Regards Knut

_______________ _______

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>JavaScri pt</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<SCRIPT language="javas cript">
function swap() {
document.getEle mentById('para1 ').value="New text";
}
</SCRIPT>
</HEAD>

<BODY>
<p id="para1">Thi s is first line</p>
<p id="para2">Her e is second line</p>
<FORM action="" method="post">
<INPUT type="button" value="Swap" onClick="swap() ">
</FORM>
</BODY>
</HTML>
_______________ _______
May 5 '06 #1
29 1720
Knut Olsen-Solberg said the following on 5/5/2006 9:39 AM:
I try to change the text in a <p> using getElementById( ). I wonder what
properties exists, and which one to use here. (The following does not
work.)


That question is asked so many times it is in the Group FAQ:

How do I modify the current page in a browser?
<URL: http://jibbering.com/faq/#FAQ4_15>

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 5 '06 #2
ASM
Randy Webb a écrit :

How do I modify the current page in a browser?
<URL: http://jibbering.com/faq/#FAQ4_15>


I am not sure to agree with this
because iCab (and Opera ?) would understand :
DocAll = (document.all?t rue:false);
and they works better with DOM
--
Stephane Moriaux et son [moins] vieux Mac
May 5 '06 #3
ASM wrote:
How do I modify the current page in a browser?
<URL: http://jibbering.com/faq/#FAQ4_15>

I am not sure to agree with this


I agree, that answer in the FAQ seems quite out-dated. It doesn't even test
whether .innerHTML is supported before setting it.

I realize FAQs are difficult to maintain, but with it being posted and cited
so often here, it really should contain better answers than this. IMO.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 5 '06 #4
On 05/05/2006 16:21, Matt Kruse wrote:

[Randy Webb:]
<URL: http://jibbering.com/faq/#FAQ4_15>

[snip]
[...] that answer in the FAQ seems quite out-dated. It doesn't even
test whether .innerHTML is supported before setting it.


True, but the linked alternative version does.

It might also be worth mentioning that, when the string doesn't contain
markup, it can be much more efficient to use the data property defined
in the W3C DOM.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
May 5 '06 #5
Matt Kruse said the following on 5/5/2006 11:21 AM:
ASM wrote:
How do I modify the current page in a browser?
<URL: http://jibbering.com/faq/#FAQ4_15> I am not sure to agree with this


I agree, that answer in the FAQ seems quite out-dated. It doesn't even test
whether .innerHTML is supported before setting it.


Did you read that section? First line:
<quote>
At its simplest in current DOM2 (with innerHTML extension) (IE5+ NS6 )
</quote>
It doesn't test for innerHTML because it specifies that it needs the
innerHTML extension. But the browser list it specifies (IE5+ NS6) is
very outdated.

But, a test for support of innerHTML doesn't mean it actually changes
the document - visibly. Search the archives for document.chicke n and you
can find those threads.

I realize FAQs are difficult to maintain, but with it being posted and cited
so often here, it really should contain better answers than this. IMO.


It links to the notes on it:

<URL: http://www.jibbering.com/faq/faq_not..._dynwrite.html >

Which is very in depth, coves testing for innerHTML (as well as it can
be tested for) and covers the problems with gEBI emulation.

Do you have a better snippet that could be used that is solid code?

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 5 '06 #6
ASM said the following on 5/5/2006 10:54 AM:
Randy Webb a écrit :

How do I modify the current page in a browser?
<URL: http://jibbering.com/faq/#FAQ4_15>
I am not sure to agree with this
because iCab (and Opera ?) would understand :
DocAll = (document.all?t rue:false);


As will IE and any other browser that supports document.all, but that is
superceded a few lines later.
and they works better with DOM


DocDom = (document.getEl ementById?true: false);
DocAll = (document.all?t rue:false);
DocStr=''
if (DocAll) DocStr="return document.all[id]"

//If the browser supports document.all, then DocStr is
//defined to use document.all

if (DocDom) DocStr="return document.getEle mentById(id)"

//If the browser supports getElementById, then DocStr is *redefined*
//to use getElementById

So, if your browser supports gEBI, then it uses it.

Its the same as writing:

if (document.getEl ementById){
//use gEBI
} else if(document.all ){
//use document.all
}

It only uses document.all if the browser doesn't support getElementById.

Although I think the way it is written is overly convoluted and
document.all support should be dropped entirely from that section, and
it becomes:

document.getEle mentById('divID ').innerHTML = 'new content';

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 5 '06 #7
ASM
Randy Webb a écrit :
ASM said the following on 5/5/2006 10:54 AM:
Randy Webb a écrit :

How do I modify the current page in a browser?
<URL: http://jibbering.com/faq/#FAQ4_15>


I am not sure to agree with this
because iCab (and Opera ?) would understand :
DocAll = (document.all?t rue:false);


As will IE and any other browser that supports document.all, but that is
superceded a few lines later.


sorry I'd read
if (DocAll) DocStr="return document.all[id]"
*else*
if (DocDom) DocStr="return document.getEle mentById(id)"

And I saw a lot of things intersting in this FAQ
--
Stephane Moriaux et son [moins] vieux Mac
May 5 '06 #8
Randy Webb wrote:
It doesn't test for innerHTML because it specifies that it needs the
innerHTML extension. But the browser list it specifies (IE5+ NS6) is
very outdated.
This isn't a very "general" approach, though.
It links to the notes on it:
<URL: http://www.jibbering.com/faq/faq_not..._dynwrite.html >
Which is very in depth, coves testing for innerHTML (as well as it can
be tested for) and covers the problems with gEBI emulation.
Do you have a better snippet that could be used that is solid code?


Perhaps a condensed version of the "alt" dynwrite, with a link to the
in-depth explanation?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 5 '06 #9
Matt Kruse said the following on 5/5/2006 1:42 PM:
Randy Webb wrote:
It doesn't test for innerHTML because it specifies that it needs the
innerHTML extension. But the browser list it specifies (IE5+ NS6) is
very outdated.


This isn't a very "general" approach, though.


Actually, I think it is more general/in-depth than it needs to be for an
FAQ Entry. The only use for the document.all code is IE4 and 1 or 2
other minimal use browsers. For an FAQ Entry, it could be nothing more than:

document.getEle mentById('conta inerID').innerH TML = newHTML;

With a link to the alt_dynwrite page.
It links to the notes on it:
<URL: http://www.jibbering.com/faq/faq_not..._dynwrite.html >
Which is very in depth, coves testing for innerHTML (as well as it can
be tested for) and covers the problems with gEBI emulation.
Do you have a better snippet that could be used that is solid code?


Perhaps a condensed version of the "alt" dynwrite, with a link to the
in-depth explanation?


Condensed:

document.getEle mentById('conta inerID').innerH TML = newHTML;

Can't get any simpler :) Anything you need to support that doesn't fit
that line of code is either so old or so obsolete that it is a lot more
work than should be required.

And personally, I think the alt_dynwrite page is incomplete/inaccurate.
It could cover inserting plain text only with something like:

function changeText(divI d,newContent){
document.getEle mentById(divId) .firstChild.nod eValue = newContent;
}

With necessary feature testing and resort to innerText and other methods
that are not as memory expensive as innerHTML. But, as some dude named
Jim Ley once said
<quote>
innerHTML is the most widely supported option, it works all over the
place, including PocketIE, IE4 etc. where nodeValue approaches won't,
it's simple to use and explain, and speed is rarely particularly
relevant.
</quote>
And that was back in 2003.......
--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 5 '06 #10

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

Similar topics

1
2670
by: Liza | last post by:
Hi, I'm a novice to postgress and need to constract a simple tree. In my table I have: regionID int parentID int name char I need to do two functions: find all the children of XXX parent and find if an item is a child of an XXX parent.
0
2026
by: Leszek Dubiel | last post by:
----------------------------------------- BACKGROUND In my company (www.glass.biz) we use ERP software to compute what has to be done to do products for our customers. Main algorithm takes data in form of two tables -- let's call them "Structure" and "Orders". "Structure" keeps information on construction
55
3751
by: Ennixo | last post by:
hi, do you know where i can find some ebooks or websites talking about C# optimisation ? for exemple, i just learned that ++i is faster than i++. i would like to know more about the things that can make code faster than fast. thank you.
12
6302
by: chadlupkes | last post by:
I've been trying to create a simple (I hope) PHP application to send emails to a membership list. I have yet to successfully connect to my database to pull the names and email addresses. I'm running XP Pro, PHP Version 4.3.2, Access 2000. I have created an ODBC System DNS connection to the database file I want to access, And I get this error: Warning: SQL error: , SQL state 00000 in SQLConnect in connecttodb.php on line 2 Whoops!...
8
5587
by: A Trujillo | last post by:
Hi group, I am looking for a simple C# editor, I would like something as simple as Notepad or GetDiz but with the highliting tool. If someone could help it would be greatly appreciated. A Trujillo
4
1603
by: Mason Barge | last post by:
I'm learning how to build a website. So far I've gotten pretty good with HTML, CSS, and Paint Shop Pro, and I'm currenly learning the basics of Javascript. I'm hoping, eventually, to build and run an online publication that will change content daily and have extensive archived articles, cross-referenced by subject area(s). What other languages would it make sense for me to learn? CGI, Perl, PHP, Ajax, SQL are all Greek to me. I do...
0
983
by: abcd | last post by:
I have web applicaiton writting in asp.net 1.1. When the first page loads it loads the data only in 2 controls. The data is poped upo by acooneting to access DSN and using ODBCConnection and executereader. With simple queries like Select Title, Name from Table1 order by Titlle Why the page loads very slowly when I request the page it takes almost 20-30 seconds to display the page. I am novice .net developer (basically, I am classic...
4
1372
by: Timmah1980 | last post by:
I'm sure this is a simple enough fix for someone out there, but I'm afraid it's beyond me! I'm putting together this simple menu for a client: http://www.timkeay.co.uk/mpc2/index.htm It works fine, but I'd like to make a couple of tweeks. As you can see, the sub-menus don't sit exactly in line with the header, so I'd like to shift them right by one pixel to correct this. I'd also like to shift
15
8022
by: Yew12 | last post by:
I am trying to create a simple login for my webpage, but Im a bit of a novice and cannot see where Im going wrong any help would be much appreciated. I'm using an access database and ODBC. The code I have got is below; <?php $tbl_name="tblCustomer";
0
9976
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
9815
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
10451
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
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8008
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7160
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5835
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
6035
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.