473,722 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing text to <DIV>

In most newer browsers, would the following methods successfully write to the <div>s below?

------
<script>
document.all.di v1.innerText = "Some random text.";

document.all.di v2.innerHTML = "More random text.";
</script>

<div id="div1">
</div>

<div id="div2">
</div>
------

If not, is there a more universal method?

Mar 29 '06 #1
8 104046

Primal-oooze wrote:
In most newer browsers, would the following methods successfully write to the <div>s below?

------
<script>
The type attribute is required:

<script type = "text/javascript">
document.all.di v1.innerText = "Some random text.";
document.all.di v2.innerHTML = "More random text.";
This would not work in all browsers because 'document.all' is
specifically for Internet Explorer.
If not, is there a more universal method?


If you want to just append text, try the following:

html:

<div id = "div1"></div>
<div id = "div2"></div>

javascript:

var myDiv1 = document.getEle mentById("div1" );
var myDiv2 = document.getEle mentById("div2" );

myDiv1.appendCh ild(document.cr eateTextNode("S ome random text."));
myDiv2.appendCh ild(document.cr eateTextNode("M ore random text."));

Don't forget to do feature detection.

Mar 29 '06 #2
Primal-oooze said the following on 3/29/2006 2:46 PM:
In most newer browsers, would the following methods successfully write to the <div>s below?
No.
------
<script>
document.all.di v1.innerText = "Some random text.";

document.all.di v2.innerHTML = "More random text.";
</script>

<div id="div1">
</div>

<div id="div2">
</div>
------

If not, is there a more universal method?


DynWrite in this groups FAQ.

<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/
Mar 29 '06 #3
Primal-oooze wrote:
In most newer browsers, would the following methods successfully write to the <div>s below?

------
<script>
document.all.di v1.innerText = "Some random text.";

document.all.di v2.innerHTML = "More random text.";
</script>

<div id="div1">
</div>

<div id="div2">
</div>
------

If not, is there a more universal method?


Greetings,

I ran into a need to update page objects pretty frequently so I wrote a
javascript function that works with all newer browsers and platforms
(that I have tested anyway). You are free to use it. I have commented
all the options that are available in the JS file.

Here is a link to the code with examples on how to use the function in
the comments:

http://www.manifestinteractive.com/u...mentControl.js

Here is a sample HTML file with some DIV's you can control using links
I have added:

http://www.manifestinteractive.com/u...ntControl.html

Hope this helps those who need it, and feel free to use any of the code
you want. I use it all the time [ cause I wrote it :) ]

- Peter Schmalfeldt

Mar 29 '06 #4
web.dev wrote:
Primal-oooze wrote:
In most newer browsers, would the following methods successfully write to ^^^^ the <div>s below?
[...]
document.all.di v1.innerText = "Some random text.";
document.all.di v2.innerHTML = "More random text.";
This would not work in all browsers


True. However, that was not what was asked.
because 'document.all' is specifically for Internet Explorer.


False. Opera always(?) implemented it, and newer Geckos implement it,
too (although it cannot be detected by feature tests in most versions).
So there is reason to believe that it works in "most newer browsers".

However, using this referencing is generally recommended against, because
both document.all and innerText/innerHTML are proprietary. Using methods
introduced with W3C DOM Level 2 Core instead is the interoperable, and I
dare say future-proof, approach.
PointedEars
Mar 29 '06 #5
>
If not, is there a more universal method?


Thanks everyone,

I googled your suggestions and have a better idea now.

I haven't worked on this stuff for a few years and I hoped by now there would be some cross browser methods.

Oh well some things never change.

Thanks


Mar 30 '06 #6
Thomas 'PointedEars' Lahn said on 30/03/2006 9:09 AM AEST:
web.dev wrote: [...]
because 'document.all' is specifically for Internet Explorer.

False. Opera always(?) implemented it, and newer Geckos implement it,
too (although it cannot be detected by feature tests in most versions).
So there is reason to believe that it works in "most newer browsers".


It 'works' in Gecko browsers only in certain circumstances:

1. If no DOCTYPE is specified.

2. If an HTML 4 Transitional or Frameset DOCTYPE is specified
*and* no link to the DTD is provided

e.g.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
It will not work in Gecko browsers if:

1. An HTML 4 Transitional or Frameset DOCTYPE is used and a
link to the loose or frameset DTD respectively is provided.

e.g.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

2. A strict DOCTYPE is used, regardless of whether a DTD link
is provided

3. An XHTML DOCTYPE is used, regardless of whether a DTD link
is provided
There may be other conditions under which it will/wont work.

--
Rob
Mar 30 '06 #7
Primal-oooze said on 30/03/2006 3:24 PM AEST:
If not, is there a more universal method?
Thanks everyone,

I googled your suggestions and have a better idea now.

I haven't worked on this stuff for a few years and I hoped by now there would be some cross browser methods.


There are.

Oh well some things never change.


They have. Use W3C DOM methods, add support for version 4 browsers if
required.

In regard to the code you posted, it won't work because you are
attempting to access and modify elements before they are created. I
guess it was just an example and what you really wanted to know was how
to go about modifying the DOM.

In regard to the methods/properties in your posted code:

document.all is a Microsoft proprietary method that is not well
supported outside IE. It has been replaced almost completely by the DOM
compliant document.getEle mentById(), which works in IE 5 and later and
virtually all browsers since about 1998.
innerText is a Microsoft proprietary property, it is not well supported
by other browsers. The DOM compliant alternative is textContent.
Alternative functions that you may like to use (or not) are posted here:

<URL:http://groups.google.c o.uk/group/comp.lang.javas cript/browse_frm/thread/a57e882da273df0/23eaaa3cc6e036e 9?q=DIV+and+Inn erHTML+differen ces+IE%2FFirefo x&rnum=1#23eaaa 3cc6e036e9>

But generally there is little reason to use it when a text node will do
the job just as well.
innerHTML is a Microsoft proprietary property that has been widely
copied by other browsers. There is Mozilla extension (the range
interface) that provides the same functionality, but it's generally
simpler to just use innerHTML.

--
Rob
Mar 30 '06 #8
Thomas 'PointedEars' Lahn wrote:
web.dev wrote:

<snip>
because 'document.all' is specifically for Internet Explorer.


False. Opera always(?) implemented it, ...

<snip>

I don't know about always but - document.all - was available in Opera
versions form 5 onwards at least, to my certain knowledge. However,
Operas 5 - 6 had an unusual attitude towards proving this feature as it
was only available (along with some other IE originating features such
as a - children - collection on Element nodes) if the browser was set to
identify itself as IE. If you set the browser to identify itself as, for
example, itself or Netscape then the DOM did not expose these features.
They gave up this DOM switching with version 7; it probably turned out
to be more effort than it was worth.

Richard.
Apr 7 '06 #9

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

Similar topics

3
6517
by: Jens Kristensen | last post by:
I have a problem displaying a divbox containing a html-textarea - everything works fine with "normal" characters. However, when the textarea contains special chars like <P> or ' , the box fails to show: function show(divbox) { divbox.style.display=''; } <div id="divbox" style="display:none" >
4
5238
by: smiley | last post by:
I hope this is the right forum for this question; i know it crosses over a few topics but here goes. I have a page that uses a server-side include perl script to return a list of name/value pairs from a text file. The last value is variable length- the shortest return is about 300 characters, and the longest is about 6000 or so. This SSI lives inside a <div> on a page with a few other text <div>s; a simplified example of the layout is: ...
3
1479
by: Bill | last post by:
Hi Testing in IE6 I have the following doctype is xhtml strict ============
7
2604
by: howa | last post by:
hi all, consider the example below: ////////////////////// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=big5" /> <title>Untitled Document</title> <style type="text/css">
28
5367
by: Kent Feiler | last post by:
1. Here's some html from a W3C recommendations page. <P>aaaaaaaaa<DIV>bbbbbbbbb</DIV><DIV>cccccccc<P>dddddddd</DIV> 2.Although I didn't think it would make any difference, I tried it with the </p>s included as well. <P>aaaaaaaaa</p><DIV>bbbbbbbbb</DIV><DIV>cccccccc<P>dddddddd</p></DIV>
2
9450
by: bgold12 | last post by:
Are there any non-semantic (i.e. actual) differences between the <p> tag and the <divtag?
1
1911
by: Num GG | last post by:
Wow... this post is pretty unreadable. Ok, i'll try to make it simpler: I've got this first page: ----------------------------------------------------------- <div id=1>some text here</div> <div id=2 style="display: none;"some text here</div> <div id=3another text here </div> <div id=4 style="display: none;">text here: <div id=4.1>one comment </div>
8
10047
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF editor. A fellow member of the PCG has helped me by creating a bit of Javascript to emulate the scrolling and using Google I've now gotten it into a state where it almost passes the W3C Markup Validation Service. However, the one error, Error Line 166,...
10
5396
by: Manikrag | last post by:
Hi Team, I searched a lot on this but could not find anything, so I am posting it here. I am using an AutoCompleteExtender in a user control which is in a master page. The page layout is designed byDIV tags. My code in that user control is somehting like.. ---------------------------- <DIV> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:TextBox ID="TextBox1" runat="server" Width="160px"></asp:TextBox>
0
8863
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
8739
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
9384
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...
0
9238
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9088
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...
1
6681
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
5995
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
4502
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...
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.