473,657 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting the inner.HTML of <DIV> with duplicate IDs...

Hello,

is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
I would like "Hello" to apear in each DIV with the ID of 'myID'. I do
now want to change the id.

Thanks in advance!
Jul 23 '05 #1
14 14954
"Charlie T" <ch************ *@gmail.com> wrote in message
news:ee******** *************** ***@posting.goo gle.com...
Hello,

is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
I would like "Hello" to apear in each DIV with the ID of 'myID'. I do
now want to change the id.


Don't!

The id attribute is supposed to be unique. To have duplicate id's on
the same page is actually wrong use of the id-tag.

see: http://www.w3.org/TR/html4/struct/global.html#adef-id

Use the name attribute if you must have equal names.

--
Dag
58°26'15.9" N 008°46'45.5" E
Jul 23 '05 #2
On Wed, 08 Sep 2004 05:43:14 GMT, Dag Sunde <ds@orion.no-way> wrote:
"Charlie T" <ch************ *@gmail.com> wrote in message
news:ee******** *************** ***@posting.goo gle.com...
is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
No, there isn't. As Dag said, using the same ID is invalid HTML and so
browser behaviour can be unpredictable.
I would like "Hello" to apear in each DIV with the ID of 'myID'. I do
now want to change the id.

You'll have to alter them. Once you do, you can use something like:

<div id="myID1">&nbs p;</div>
<div id="myID2">&nbs p;</div>

var getRefById = function() {return null;};
if(document.get ElementById) {
getRefById = function(i) {return document.getEle mentById(i);};
} else if(document.all ) {
getRefById = function(i) {return document.all[i] || null;};
}

function changeContents( ) {
var o, nD = 2; // Number of DIVs

for(var i = 1; i <= nD; ++i) {
o = getRefById('myI D' + i);

if(o) {o.innerHTML = 'Hello';}
}
}

[snip]
Use the name attribute if you must have equal names.


That isn't any better. The DIV element doesn't have a name attribute.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
"Charlie T" <ch************ *@gmail.com> wrote in message
news:ee******** *************** ***@posting.goo gle.com...
Hello,

is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
I would like "Hello" to apear in each DIV with the ID of 'myID'. I do
now want to change the id.


IDs *must* be unique within the page so you will have to change them.

The following code, I've been told, loops through all DIV elements with the
class name "myclass" and changes the text colour to red.

Would it be possible to change the id="myId" to class="myclass" on your page
then change the routine below so it changes the innerHTML of each element
listed in divs[i] instead of the text colour?

var divs = document.getEle mentsByTagName( 'div');
if (divs) {
for (var i = 0; i < divs.length; i++) {
if (divs[i].className == 'myclass') {
divs[i].style.color = 'Red';
}
}
}
Jul 23 '05 #4
"Charlie T" <ch************ *@gmail.com> wrote in message
news:ee******** *************** ***@posting.goo gle.com...
Hello,

is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
I would like "Hello" to apear in each DIV with the ID of 'myID'. I do
now want to change the id.


As an update to my earlier reply...

Change id="myID" to class="myclass" then stick the following function
wherever it's wanted.
It seems to work in the IE6, Opera7.54 and Netscape7.1

function changeDiv()
{
var divs = document.getEle mentsByTagName( 'div')
if (divs)
{
for (var i = 0; i < divs.length; i++)
{
if (divs[i].className == 'myclass')
{
divs[i].innerHTML = "New Text"
}
}
}
}
Jul 23 '05 #5
On Wed, 8 Sep 2004 10:33:37 +0100, Danny@Kendal
<da***@STOPSPAM ghpkendal.co.uk > wrote:

[snip]
function changeDiv()
{
var divs = document.getEle mentsByTagName( 'div')
if (divs)
That shouldn't be necessary. If there are no matching elements, gEBTN must
return an empty collection. If that's the case, the for loop will be
skipped.

That said, it is necessary to feature test for gEBTN before calling it.
{
for (var i = 0; i < divs.length; i++)
{
if (divs[i].className == 'myclass')
{
divs[i].innerHTML = "New Text"
}
}
}
}


This would be better written as:

function changeDiv() {
if(document.get ElementsByTagNa me) {
var divs = document.getEle mentsByTagName( 'DIV');
for(var i = 0, n = divs.length; i < n; ++i) {
if('myclass' == divs[i].className) {
divs[i].innerHTML = "New Text";
}
}
}
}

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
Thanks guys!

It seems to work, but my only fear is that if I decide to use CSS
where will I put the class?

What if I did this...
<span class="CSS">
<DIV class="myclass" ></DIV>
</span>

will this work?..hmm

Any ideas?

-Charlie
Jul 23 '05 #7
"Charlie T" <ch************ *@gmail.com> wrote in message
news:ee******** *************** ***@posting.goo gle.com...
Thanks guys!

It seems to work, but my only fear is that if I decide to use CSS
where will I put the class?

What if I did this...
<span class="CSS">
<DIV class="myclass" ></DIV>
</span>

will this work?..hmm

No.

You put it in your .ccs file, or in a <style>
section at the top of your document.

--
Dag
58°26'15.9" N 008°46'45.5" E
Jul 23 '05 #8
On Wed, 08 Sep 2004 19:17:23 GMT, Dag Sunde wrote:

(styles)
You put it in your .ccs file, ..


Of course, Dag meant '.css'* there.

[ * Cascading Style Sheets ]

Although the exact file extension *usually*
does not matter, sometimes browsers will
fall back to the file type to try and guess
what to do with it.

It's best to give the browser every hint possible.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Jul 23 '05 #9
On 8 Sep 2004 12:12:49 -0700, Charlie T <ch************ *@gmail.com> wrote:
Thanks guys!

It seems to work, but my only fear is that if I decide to use CSS
where will I put the class?
Pardon? Where did anyone mention CSS?
What if I did this...
<span class="CSS">
<DIV class="myclass" ></DIV>
</span>

will this work?..hmm


It depends what you're trying to achieve by doing it. As I don't know
where this line of questioning came from (CSS shouldn't have anything to
do with this issue), I couldn't guess.

By the way, the HTML you just presented is invalid. SPAN elements can only
contain inline elements (text, form controls, images, etc). DIV is a block
element and can only be contained by other block elements.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #10

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

Similar topics

1
2501
by: Philo | last post by:
How do I select all <div> tags except those which contain a <table> tag somewhere within them? Example XML: <********************** sample input ***********************> <txtSectionBody> <div> <span>
3
84599
by: Paul Thompson | last post by:
When I put a <div ...> inside a <table> specification, functionality is not there. When I put the <table> inside the <div> everything works. Why is that?
8
14461
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a page, there is a huge gap (like 3/8 inch or 25 px) between them. This is driving me bananas. What the hey am I missing? dh ------------------------------------------------ Dan Hansen ------------------------------------------------
8
5140
by: slim | last post by:
hi again all, i am still working on the website as mentioned in earlier threads and have hit another snag... http://awash.demon.co.uk/index.php http://awash.demon.co.uk/vd.css the php is pulling a name and placing it under the thumbnail (the text is
5
13156
by: hibernate | last post by:
I'm somewhat new to javascript/DHTML, and this problem has been plaguing me. I have made an 'array' of <div> tags within my html document like so: <div id="menu"> menu1 </div> <div id="menu"> menu2 </div> <div id="menu"> menu3 </div> <div id="menu"> menu4 </div> <span onclick="showOrHide('menu')"> show/hide menu1 </span>
2
2057
by: Nicky | last post by:
hi, all I know we can do this by some jscript. But is there a way to do it in asp.net c# code? In our project, users could sumit a piece of html code and I need to remove all html tag out. What's the better way to do this? Thanks and have a wonderful holiday!
7
3616
by: pamelafluente | last post by:
The precious input given by Laurent, Martin, Benjamin about XMLHttpRequest in Javascript, has made me think that perhaps I could improve what I am currently doing by using Ajax. Let's make it simple and schematic, to see if there is a simple Ajax answer to this. A. I have an HTML page which has some pure html/css code representing a GRID of cell. The page may also contain other objects (images, etc). B. On the server I have a windows...
5
38998
by: chakradhari.ashish | last post by:
Does anybody know how can I load an html page inside a <div</div> where the content gets update on the onclick even of anchor <a href=> </a>? I googled around and followed some links pointing me that it is possible to use javascript combined with CSS for doing this? Any ideas? Chakra
0
1851
by: Patricia Mindanao | last post by:
Assume I have a HTML web page with a pre-defined <div...</divarea. When the users clicks now on a certain link on this web page (outside or inside this "div" area) then the content of a file say "first.html" (currently still on the server) should be loaded and filled into the existing <div>...</divarea in the current Web page and displayed. How can I do this in detail? Pat
0
8844
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
8518
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
7354
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
6177
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
5643
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
4173
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
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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
2
1734
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.