473,248 Members | 1,446 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,248 software developers and data experts.

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 14909
"Charlie T" <ch*************@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
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
5826'15.9" N 00846'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.google.c om...
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">&nbsp;</div>
<div id="myID2">&nbsp;</div>

var getRefById = function() {return null;};
if(document.getElementById) {
getRefById = function(i) {return document.getElementById(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('myID' + 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.google.c om...
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.getElementsByTagName('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.google.c om...
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.getElementsByTagName('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***@STOPSPAMghpkendal.co.uk> wrote:

[snip]
function changeDiv()
{
var divs = document.getElementsByTagName('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.getElementsByTagName) {
var divs = document.getElementsByTagName('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.google.c om...
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
5826'15.9" N 00846'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
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message news:<opsd0xpth3x13kvk@atlantis>...
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


My question was... if I use the class to populate the innerHTML of an
object then what would I use if I wanted to create a 'CSS' for it.

Then I suggested nesting my DIV inside of another tag with the class I
need for CSS....

get it?

Thanks for all the help
Jul 23 '05 #11
Charlie T wrote:
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message news:<opsd0xpth3x13kvk@atlantis>...
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

My question was... if I use the class to populate the innerHTML of an
object then what would I use if I wanted to create a 'CSS' for it.


Why not the class you assigned to it?
Then I suggested nesting my DIV inside of another tag with the class I
need for CSS....
If you nest the DIV, nest it inside another DIV. But as Michael said, it
seems overkill to nest two DIV's just to be able to script it. Give them
unique ID's and the problem is solved.
get it?


Can I get back to you on that one?
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #12
Charlie T wrote:
What if I did this...
<span class="CSS">
<DIV class="myclass"></DIV>
</span>

will this work?..hmm


That's unlikely, it is not Valid HTML.

<http://validator.w3.org/>
PointedEars
--
It's get'in so as a businessman can't expect no return from a fixed fight.
If you can't trust a fix, what can you trust?
Jul 23 '05 #13

Mike,

I'm intrigued by your use of

for(var i = 0, n = divs.length; i < n; ++i) {
if('myclass' == divs[i].className) {

rather than the equivalent and shorter:

for(var i = 0; i < divs.length; ++i)

Is there any reason
-
Rob

Jul 23 '05 #14
On Sun, 12 Sep 2004 21:42:41 -0500, RobG
<Ro*********@mail.forum4designers.com> wrote:
I'm intrigued by your use of

for(var i = 0, n = divs.length; i < n; ++i) {
if('myclass' == divs[i].className) {

rather than the equivalent and shorter:

for(var i = 0; i < divs.length; ++i)
They aren't always equivalent; it depends on what happens during execution
of the loop. If the length of the array/collection can change, there is a
big difference in behaviour. But, as I'm simply iterating through the
collection, there is no need to continuously resolve an unchanging
property. Instead, I can look it up once and store the value.
Is there any reason?


I suppose there's little to be gained in Javascript, unless the executing
machine is slow or the collection is very large[1], so it's habit, mainly.
However, I consider it a good idea to get as much performance as possible
from event-driven code.

Mike
[1] Either is possible on the Web. Getting a collection of frequently used
elements within a document could easily return hundreds of items.

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

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

Similar topics

1
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>...
3
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
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...
8
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...
5
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">...
2
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...
7
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...
5
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...
0
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...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.