[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
I knew about :input, but my brain just pre-supposed it meant only input elements so I had kind of disregarded them somewhat. So thanks for pointing that out!
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
On Feb 11, 2008 10:07 AM, Dave Stewart [EMAIL PROTECTED] wrote: Hi Aaron, Good styles there with the name attribute stuff. Not so good if you want to grab other entities such as selects This should select any form element (input,select,textarea,button) whose name attribute is foo[bar][baz]: $(:input[name='foo[bar][baz]']) You're gonna wanna restrict it, though, so it doesn't search the *entire* page. Something like: $(#myFormsId :input[name='foo[bar][baz]']) or $(:input[name='foo[bar][baz]'], domElementForMyForm) You can read more about jQuery selectors at http://docs.jquery.com/Selectors -- Aaron Heimlich Web Developer [EMAIL PROTECTED] http://aheimlich.freepgs.com
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
Hey Marty, I couldn't live without PHP's square brackets. I just about always want to split up a form into component parts. I could easily write something using RegExp to do this for me, but having it all built in is just lovely. Great that Rails has it too. Cheers, Dave
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
Hi Aaron, Good styles there with the name attribute stuff. Not so good if you want to grab other entities such as selects, but good to see that it accepts non-standard characters.
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
All Browsers assign form data variable names according to the name attribute, not id. PHP uses square brackets as a shortcut to automagically build arrays from the request data. Like Karl said, this is illegal in HTML, and still seems not allowed in XHTML according to http://www.w3.org/TR/2008/PER-xml-20080205/#sec-common-syn (XHTML is XML, remember). That section has a paragraph beginning The ASCII symbols and punctuation marks, along with a fairly large group of Unicode symbol characters, are excluded from names..., which leads me to believe square brackets are still disallowed in XHTML names. The allowed unicode ranges given exclude the ascii range #x5B to #x7F (square brackets are #x5B and #x5D). Assuming I'm reading the spec correctly. Most of the expanded characters in XML seem to be in a much higher range than ascii. Personally, I wish PHP didn't do this with square brackets. On Feb 7, 2008 4:29 PM, Dave Stewart [EMAIL PROTECTED] wrote: OK - this is the best I could come up: function $$(selector, context){ return jQuery(selector.replace(/(\[|\])/g, '\\$1'), context) } $$('#contact[email]') It adds to the global namespace (so won't work with prototype for example, which also uses $$) but it does do trick. Thoughts?
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
Actually, according to the HTML 4.01 spec, the name attribute on input elements[1] is defined as being of type CDATA[2], which is very permissive in the kinds of characters it allows. There is a section in the XHTML 1.0spec that talks about restricting the allowed characters in the name attribute[3], but since it's talking about fragment identifiers, I'm not sure whether it applies to form field names or not (that would suck if it did, though). As for the OP's problem, this has worked beautifully for me: $(input[name='foo[bar][baz]']) // notice the single quotes around foo[bar][baz] [1] http://www.w3.org/TR/html401/interact/forms.html#adef-name-INPUT [2] http://www.w3.org/TR/html401/types.html#type-cdata [3] http://www.w3.org/TR/xhtml1/#h-4.10 On Feb 8, 2008 10:53 AM, Marty Vance [EMAIL PROTECTED] wrote: All Browsers assign form data variable names according to the name attribute, not id. PHP uses square brackets as a shortcut to automagically build arrays from the request data. Like Karl said, this is illegal in HTML, and still seems not allowed in XHTML according to http://www.w3.org/TR/2008/PER-xml-20080205/#sec-common-syn (XHTML is XML, remember). That section has a paragraph beginning The ASCII symbols and punctuation marks, along with a fairly large group of Unicode symbol characters, are excluded from names..., which leads me to believe square brackets are still disallowed in XHTML names. The allowed unicode ranges given exclude the ascii range #x5B to #x7F (square brackets are #x5B and #x5D). Assuming I'm reading the spec correctly. Most of the expanded characters in XML seem to be in a much higher range than ascii. Personally, I wish PHP didn't do this with square brackets. On Feb 7, 2008 4:29 PM, Dave Stewart [EMAIL PROTECTED] wrote: OK - this is the best I could come up: function $$(selector, context){ return jQuery(selector.replace(/(\[|\])/g, '\\$1'), context) } $$('#contact[email]') It adds to the global namespace (so won't work with prototype for example, which also uses $$) but it does do trick. Thoughts? -- Aaron Heimlich Web Developer [EMAIL PROTECTED] http://aheimlich.freepgs.com
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
Pre jquery I have used an id without the square brackets and a name with the square brackets. I used the id in my javascript and the form would submit with the name. Ken On Feb 7, 6:28 am, Dave Stewart [EMAIL PROTECTED] wrote: I'm finding it impossible using jQuery to select any attributes with square brackets in them. I'm sure most people know PHP uses square brackets within form element names to submit multi-dimensional arrays, ie: contact[name] contact[email] contact[telephone] contact[options][option 1] contact[options][option 2] contact[options][option 3] This is read natively by PHP as: contact = array ( name, email, telephone, options = array( option 1, option 2, option 3 ) ) In a way, jQuery's refusal to recognise square brackets makes sense, as it leans towards attribute selectors (which are fantastic tools), but vanilla getDocuementById() works, so I'm wondering why jQuery hasn't some kind of check for this kind of thing? In the meantime, does anyone have any best-practice workarounds to this issue, for example for the above data structure? Combined selectors, e.g. #contact-email and separate selectors e.g. #contact #email offer different advantages. Has anyone any preferences, and why?? Many thanks, Dave Stewart
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
OK - this is the best I could come up: function $$(selector, context){ return jQuery(selector.replace(/(\[|\])/g, '\\$1'), context) } $$('#contact[email]') It adds to the global namespace (so won't work with prototype for example, which also uses $$) but it does do trick. Thoughts?
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
You can use square brackets, you just have to put a '\' before them (which has to be done as '\\' because of JavaScript's encoding) http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_select_an_element_that_has_weird_characters_in_its_ID.3F Karl On Feb 8, 2008 1:28 AM, Dave Stewart [EMAIL PROTECTED] wrote: I'm finding it impossible using jQuery to select any attributes with square brackets in them. I'm sure most people know PHP uses square brackets within form element names to submit multi-dimensional arrays, ie: contact[name] contact[email] contact[telephone] contact[options][option 1] contact[options][option 2] contact[options][option 3] This is read natively by PHP as: contact = array ( name, email, telephone, options = array( option 1, option 2, option 3 ) ) In a way, jQuery's refusal to recognise square brackets makes sense, as it leans towards attribute selectors (which are fantastic tools), but vanilla getDocuementById() works, so I'm wondering why jQuery hasn't some kind of check for this kind of thing? In the meantime, does anyone have any best-practice workarounds to this issue, for example for the above data structure? Combined selectors, e.g. #contact-email and separate selectors e.g. #contact #email offer different advantages. Has anyone any preferences, and why?? Many thanks, Dave Stewart
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
Wow, that's really useful to know, thanks Karl. I think I'll just use a regular expression: selector = selector.replace(/(\[|\])/g, '\$1') It would be really useful if this were an option, somehow. My jQuery- foo is not all that. Any ideas, anyone? Cheers, Dave
[jQuery] Re: jQuery won't recognise attribute names containing [square brackets]
Remember also that technically in HTML id and name attributes can't contain '[]'s. ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (-), underscores (_), colons (:), and periods (.). - http://www.w3.org/TR/html401/types.html#type-name In XHTML name can contain a lot more though. ( http://www.w3.org/TR/2002/REC-xhtml1-20020801/#C_8 ) Browsers have relaxed the HTML rules and allowed name to contain whatever it can contain in XHTML. PHP uses the name attribute rather that id and so it gets away with it because the relaxed rules. Karl Rudd On Feb 8, 2008 10:14 AM, Dave Stewart [EMAIL PROTECTED] wrote: Wow, that's really useful to know, thanks Karl. I think I'll just use a regular expression: selector = selector.replace(/(\[|\])/g, '\$1') It would be really useful if this were an option, somehow. My jQuery- foo is not all that. Any ideas, anyone? Cheers, Dave