Jump to content

IE and document.getElementByName()


jackpf

Recommended Posts

Has anyone noticed how IE will also get elements by id whilst using by name?

This is infuriating, as IE and Firefox will obviously point to different elements for different indexes of the array of element's names.

 

For example:

<div id="something"></div>
<div name="something"></div>

In Firefox, document.getElementsByName('something')[0] returns the second div. In IE it returns the first.

 

Has anyone found a workaround or anything?

I mean...IE sucks...but this is just stupid. There is a distinct difference between name and id.

I hate IE...

 

Regards,

Jack.

Link to comment
Share on other sites

Well, at the moment I check to see if the attribute's value is undefined. If so, it moves on to the next one. But...this is rediculous. Why can't IE differentiate between an ID and a Name?

I've noticed that Opera does this as well. Does Opera use IE's javascript engine or something? Firefox and Safari are fine.

Link to comment
Share on other sites

Well, I have IDs and Names that all relate to eachother. It just makes it easier for me to remeber what's what when I'm recoding it. I'd rather not change my code just because IE can't execute javascript properly.

 

An ID is an ID, a Name is  a Name. How hard can it be?

Link to comment
Share on other sites

whenever I'm coding html, I always make pretend that id and name are one in the same.  So if I do

 

<div id='blah'>..</div>

 

no other tag/element will have 'blah' as an id OR name. So whether something has an id, name or id AND name, it will always be the same thing.  like

 

<input id='something' name='something ..>

 

etc...

 

and no other thing on the page will ever have 'something' as an id OR name.  That way, I don't  have to worry about stuff like this.

 

I don't see how that's any harder to remember than what you are doing.  I can understand not wanting to go back and redo your code, but moving forward, if you do that in the first place, then it won't be a problem.

Link to comment
Share on other sites

Yeah, I guess.

But what I usually do is give a relevant name/id to my forms and their parts.

So for example, the form to post on my forum, is something like this:

<form id="post" action="..." method="post">
<textarea name="post"></textarea>
<input type="submit" name="submit" />
</form>

 

It just makes sense that way. I often use javascript on my forms, so the form id allows me to do that, and obviously I need the names for PHP.

I guess it's preference really. Either way, IE is still doing it wrong though :P

Link to comment
Share on other sites

hmm...well tbh I think you should be using better naming conventions.  For instance, if the form was a "contact us" form, I would do this:

 

<form id="form_contactUs" name="form_contactUs" action="..." method="post">
  <textarea id="form_contactUs_message" name="form_contactUs_message"></textarea>
  <input type="submit" name="form_contactUs_submit" id="form_contactUs_submt" />
</form>

 

Now, whether I would actually have id AND name in all of those depends on my coding needs, but regardless of whether I do or not, I will not have any other tag/element use those names/ids.

Link to comment
Share on other sites

Meh...long names though :P

And as I understand it the use of the name attribute in a form is decpricated.

Ahh well, my javascript is working now anyway; just a  bit of a pain that I have to check each element to see if it exists because of IE.

Link to comment
Share on other sites

long names, yes, but it's not like your processor is really suffering from it, and better organization makes all the difference in the world when trying to debug or edit/modify later, especially when it's not you that's the one doing it.  Good programmers do not just think in the present.  They think about the future, too. I spend all day wading through other people's shit, wishing that they would have just put the extra 10 seconds worth of effort it takes to do basic shit, like name stuff better, indent, comment, etc...

Link to comment
Share on other sites

I personally find my own method more organisational, but obviously that's because it's what I've always used. Probably the same for you.

 

And yeah...but I doubt my site's going to get big enough to have other people working on it any time soon...lol.

Link to comment
Share on other sites

well, your site is your own little sandbox, though.  What if you start taking on clients, writing scripts for them, modifying stuff on their site, etc.. where you may not be the first or last programmer to mess with it?  I mean, that's the core reason why people push for standards: so people can be on the same page and know what's going on and follow each other's work.  I mean, that is the central theme of this very thread: MS dropping the ball on shit, right?  Well, you have to practice what you preach...

Link to comment
Share on other sites

Well yeah, but to me my method does seem organised. Naming a form and it's elements in a similiar fashion makes sense to me.

 

Ok, this is my exact post form, taken from my script.

<?php
echo '<span class="action">Post</span>
<form id="post" action="'.$_SERVER['REQUEST_URI'].'" method="post"><div>
	<textarea class="post" name="post" onkeydown="return tab(this, event);"></textarea><br /><br />
	<input type="button" class="post" value="Preview" onclick="AJAX(\'post\');" /><input type="submit" name="submit" class="post" value="Post" />
	'.parse_display().'
	<div id="ajax"></div>
</div></form>';
?>

 

To me, this seems organised. But IE has trouble with my AJAX() function, which uses getElementsByName(). It picks up the form as the first element. As there is only one element actually named "post", I should be able to use [0] as the correct index, but this doesn't work for IE.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.