/    Sign up×
Community /Pin to ProfileBookmark

Two Matching IDs (won’t validate)

Hello,
I am working on a website and have 2 id’s that match. However, all I am trying to do is validate the code.

The second time I use the ID, I have to, because the JavaScript in my code needs to read the ID. It is unable to read a class attribute, and the rel attribute won’t work either, since I am using custom values. Therefore, it has to be an ID attribute.

However, if the JavaScript reads the ID, then the first time I use the id, it also must be an ID. This puts me in an awkward position of having to have 2 ID attributes.

Basically, I need to know if there is a way to validate while having 2 IDs. Otherwise, is there a way to validate a ‘rel’ attribute with a custom value?

Thank you very much.

-TitanVex

to post a comment
HTML

11 Comments(s)

Copy linkTweet thisAlerts:
@holyhttpMay 01.2012 — An ID needs to be unique in a given web page: that's the whole point of it. Use classes. Javascript can ready classes (getElementsByClassName) but will return an array.

So by using the same ID twice you are violating an important design principle and it will cost you dearly later if you do not make the proper corrections now.
Copy linkTweet thisAlerts:
@aj_nscMay 01.2012 — IE<9 doesn't have the getElementsByClassName method.

@Titan, HTML IDs must be unique, no if's and's or but's about it. Not only is it not valid HTML to duplicate an ID, browser behaviour can easily be unexpected as no browser is supposed to support the possibility of getElementById() matching more than one element (hence get[B]Element[/B]ById and get[B]Elements[/B]ByTagName).

You can always use a different ID and use both ID's in your JavaScript code. But that might not work depending on exactly what you are trying to do. Could you explain in a little more detail what it is that you are trying to accomplish?
Copy linkTweet thisAlerts:
@CharlesMay 01.2012 — IE<9 doesn't have the getElementsByClassName method.[/QUOTE]Nor does the DOM spec, I believe. But it's easy enough to make your own getElementsByClassName--and easier still if you aren't using multiple classes with the elements that you are looking for.
Copy linkTweet thisAlerts:
@TitanVexauthorMay 01.2012 — Thank you everyone for your help. It isn't a huge deal that I have it validated (this is just for a project), but I would feel better about it being a professional website.

Here is the link to the site that uses the JavaScript:

http://students.uwsp.edu/ntowl795/cnmt210/finalproject/about.html

And here is my code (I have truncated it, just so you can get the picture):
[code=html]<div class="pane">
<p>
<h1>Missions</h1>
<h5>Select a Mission to the left to see where it is located on the map.</h5>
<div class="menu"></div>
<div class="demo1">
<img src="images/world_map.jpg" class="imgMap" alt="worldmap"/>

<div class="marker" id="mis1" data-coords="475, 540">
<h3>Operation 40</h3>
<br>
<br>
<p >
Plan a very risky assassination during the Bay of Pigs Invasion.
</p>
</div>
<div class="marker" id="mis2" data-coords="1246, 214">
<h3>Escape</h3>
<br>
<br>
<p>
Vorkuta which is a brutal Soviet labor camp that you must escape from, if you didn't catch the camp itself is named Vorkuta.
</p>
</div>
<div class="marker" id="mis3" data-coords="510, 430">
<h3>U.S.D.D.</h3>
<br>
<br>
<p>
Meet up with top senior officials in US soil to discuss about the threat posed by General Dragovich.
</p>
</div>

</div>
<div class="controls">
<a href="#" id="mis1">Mission 1</a>
<a href="#" id="mis2">Mission 2</a>
<a href="#" id="mis3">Mission 3</a>

</div>
</div>[/code]


I tried changing the JavaScript to "class", rather than "id", but I continuously got errors. Thank you all once again for your help, I really appreciate it.
Copy linkTweet thisAlerts:
@JPnycMay 01.2012 — There's more to this than just not validating. The script simply won't work with duplicate IDs. They must [B]never [/B]be duplicated under any circumstances. IDs MUST be unique on each page, period.
Copy linkTweet thisAlerts:
@TitanVexauthorMay 01.2012 — There's more to this than just not validating. The script simply won't work with duplicate IDs. They must [B]never [/B]be duplicated under any circumstances. IDs MUST be unique on each page, period.[/QUOTE]

Well it works for me right now. Originally, I had the "rel" attribute to the <a> tag. But that wouldn't validate either. I am not sure what I would have to do to make this both work AND validate.
Copy linkTweet thisAlerts:
@holyhttpMay 01.2012 — All 4 people who tried to help agreed on the fact that an ID should not be duplicated in a web page. Just because "Well it works for me right now" for some web browsers is not a valid reason to proceed that way. As I mentioned it will cost you a lot more and you are already experiencing the consequences.

Do yourself a favor and make the corrections to your JavaScript code to use CSS classes.
Copy linkTweet thisAlerts:
@TitanVexauthorMay 01.2012 — All 4 people who tried to help agreed on the fact that an ID should not be duplicated in a web page. Just because "Well it works for me right now" for some web browsers is not a valid reason to proceed that way. As I mentioned it will cost you a lot more and you are already experiencing the consequences.

Do yourself a favor and make the corrections to your JavaScript code to use CSS classes.[/QUOTE]


I know that. That is what I am trying to do. That is why I made this thread, so I could fix the problem. I want to know if there is any way to fix it. Because changing the 'id' to a 'class' doesn't work. And it won't validate if it is 'rel' either.

Does anyone have suggestions?
Copy linkTweet thisAlerts:
@CharlesMay 01.2012 — Does anyone have suggestions?[/QUOTE]Make your own getElementsByClassName. Will you be searching for instances where a single element belongs to more than one class?
Copy linkTweet thisAlerts:
@JPnycMay 01.2012 — Where is the dupe ID in that code? I don't see it.
Copy linkTweet thisAlerts:
@TitanVexauthorMay 01.2012 — Where is the dupe ID in that code? I don't see it.[/QUOTE]

Near the top I have:
[code=html]<div class="marker" id="mis1" data-coords="475, 540">
<h3>Operation 40</h3>
<br>
<br>
<p >
Plan a very risky assassination during the Bay of Pigs Invasion.
</p>
</div>[/code]


And near the bottom I have:
[code=html]<div class="controls">
<a href="#" id="mis1">Mission 1</a>
<a href="#" title="b">Mission 2</a>
<a href="#" title="c">Mission 3</a>

</div>[/code]


I duplicated [B]id="mis1"[/B]
×

Success!

Help @TitanVex spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...