开发者

Possible Javascript selector (no framework) to get array of elements using array key naming syntax

开发者 https://www.devze.com 2023-04-08 22:25 出处:网络
I have a form where the inputs have names such as: unit_price[1] unit_price[2] The only way I\'ve found to access them from javascript is using:

I have a form where the inputs have names such as:

unit_price[1]
unit_price[2]

The only way I've found to access them from javascript is using:

document.getElementsByName("unit_price[1]")[0]

I was wondering开发者_JAVA百科 if there is a way to access them as a single array in one selector.

I'm looking for a pure javascript way to do this, but the page does have the YUI 2 framework loaded, in case there is a one step way of doing it using yui syntax.


From the YUI2 docs:

var nodes = YAHOO.util.Selector.query('input[name^=unit_price]');


Here's a native solution using querySelectorAll()[docs] :

document.querySelectorAll("[name^=unit_price]");

Has pretty good browser support.

http://www.quirksmode.org/dom/w3c_core.html

Possible Javascript selector (no framework) to get array of elements using array key naming syntax

Good idea to prefix the selector with input like @davin did in his answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号