Hey guys,
Here''s my quick scenario. I''ve built a simplistic function to
generate
and return a CSS selector for whatever input is provided. If an
element is passed to this function as an argument, a full CSS selector
is returned. However, for added fun in the sun, I''ve made it so that
if a string is passed to this function and it already is a CSS
selector, that selector itself will be returned. Then, in the interest
of making this function work a little bit like Prototype''s Element
methods that all can take a string as it''s first argument representing
an element ID in the DOM, I made my function check to see if an
element exists with that ID and return it''s CSS selector, or as a
fallback if the element does not exist in the DOM, return the string
provided prefixed by a ''#'' character.
So, assuming a simple HTML node like this exists in the page:
<div id="eric" class="neato">Something</div>
... my function works like this:
getSelector(''eric'');
// returns ''div#eric.neato''
getSelector($(''eric''));
// returns ''div#eric.neato''
getSelector(''div#something_else'');
// returns ''div#something_else''
getSelector(''something_else''); /* not in the DOM */
// returns ''#something_else''
Now, all of this so far is _EXACTLY_ what I intended this function to
do. The problem is when I''m trying to use this function to return a
selector for an element by passing a string of the element''s name.
getSelector(''input''); /* return a CSS selector for all
inputs */
// returns ''#input''
That obviously is completely broken and doesn''t do what I want at all.
So, I quickly tried to find a mechanism to check to see if a string
passed (without any non-word characters /\W/) is actually the name of
a valid HTML entity. So, in Firefox, I quickly was able to develop
this little mechanism to test the string like this:
var test = document.createElement(str);
if ( test.constructor == window.HTMLUnknownElement ) {
/* invalid HTML entity, this must refer to an ID so return
''#'' + str */
} else {
/* this is a valid HTML entity, so just return the string */
}
That works PERFECTLY... in Firefox. Internet Explorer does not have an
element constructor in the window object for HTMLUnknownElement, nor
does it have any other indication to let me know that this element is
not legal. Internet Explorer says that the .nodeType is 1 for this
element and is more than happy to treat this element as a
''valid''
element.
This behavior frustrates me a LOT and I haven''t been able to find any
sort of workaround that will solve this problem. The obvious
alternative would be to create a huge array of valid HTML elements and
just check it to see if the string passed to my function exists in
that array, but that just seems like a nasty hack and I''d like to
avoid it if possible.
Does anyone know if there is anything I can do to find out if a string
is a valid HTML element in Internet Explorer?
Thanks in advance,
-E
--
Eric Ryan Harrison
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---