psuplat;10997157 wrote:
Now, the actual problem lies in how IE understands GetElementById property.
In this case, I'd rather say the problem is that you assume that getElementById should get elements by anything other than id. It's not until you start assuming that something should do what it actually is supposed to do that non-standard conformance becomes a problem.
psuplat;10997157 wrote:
The IE the will get the id property by using the name attribute of the <input />,
No, it really will not. It will assume that you want to get an element with the name attribute matching the string you specified when claiming that you wanted to get an element with a matching id attribute. But it will not get the matching element's id attribute, since that element didn't have one to begin with.
Moreover, you should put header('x-ua-compatible: ie="edge"'); in your documents to tell IE 8 and 9 to actually behave as IE 8 or 9, rather than IE6 or 7. And IE8 will not get an element by its name if getElementById('id') fails to find an element with a matching id unless the user tells it to use "compatibility mode" (which actually should be called "as far off from standards mode as Microsoftly possible).
Anyway a good way to not having to deal with these problems is starting to use jQuery. I learnt javascript before I started looking at jQuery, and it's entirely possible to both learn this way and be successful in achieving what you want. But in the beginning you will also spend a lot of time on figuring things out. First, you need to spend time on a problem before you realize it's not your code which is "wrong" (according to standard - if your code doesn't do what you want it to, it actually is wrong), but that one or more browsers is out of line and need to be accomodated. After that you need to figure out how to do this (web search is usually faster than trial and error). Then after a while, you will from time to time assume that a browser is out of line until you realize that it's your code that was wrong in the first place, thus making you lose time due to an erroneous assumption.
But if you do decide to learn javascript, at least do yourself the favor of using a browser with reasonably high standards compliance. That pretty much means any browser but IE. I havn't really had a look at IE9 (and I do not plan to, since I will use jQuery to deal with that instead), but from what I have seen, it is a lot better than earlier versions. Still, it will give you no advantage at all when it comes to the earlier versions anyway. Either IE9 has the same crap behaviour as they did, some other crap behaviour than the others, or it is following the standard which you can't assume that earlier versions would. My personal recommendation would be to use Firefox and installing the Firebug plugin. Other browsers have similar tools, but I still find Firebug easier to use. Also, Mozilla's documentation is not only the best js documentation I've found but it also often points out when other browsers need to be handled in different ways or tells you if something in FF is not following the standard or is an addition to it. Just never assume that their docs will always provide this information. You will need to test in every single browser you care to support.
But I'd still rather recommend doing it the easy way: use jQuery and avoid a whole lot of problems. Once you are comfortable with jQuery, you could of course still get around to learning core javascript, should you feel the need. But I really doubt you will. Should you ever encounter something you wish to do and it isn't possible using jQuery, it is very easy to return to core javascript. You can always turn a specific jQuery object into its corresponding DOM node using jqobject.get(0).
In this case, you'd get the same behaviour from all browsers using
var el = $('#id');
In all browsers, this would only find an element having id="id", never name="id". This would still hold if you set IE8 to compatibility mode, since
function find(f)
{
var el = $('#name');
var el = el.get(0);
alert(el);
}
would show an alert with text "undefined".
And if you really want to use core javascript while talking about "making things work", you should NOT be talking about FF or Chrome at all to begin with since they allready work like they are supposed to work. What you should say is
"To make it work as it should in certain versions of IE or setting other IE versions to use compatibility mode, I need to"
var name = 'going_after_name_rather_than_id';
var el =getElementById(name);
if (el && el.id != name)
el = undefined
since this would give you consistent and standard compliant behaviour across browsers.