1

I have multiple elements that have ID's (and names) in the form :

id="foo[1]"
id="foo[2]"

However, I can't seem to get the value back out using:

$('#foo[1]').val();

What's the correct syntax for accessing these multi-dimensional arrays in Jquery.

Thanks.

niggles
  • 1,010
  • 1
  • 9
  • 21

6 Answers6

2

$('[id=foo\[1\]]') will select those sort of ids

http://jsfiddle.net/UaxC2/

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
1

I would recommend using id="foo_1" and id="foo_2" instead of the brackets (it will play better).

Then the correct syntax is: $('#foo_1').val() (you are missing the "#")

Edit: Looks like your ids aren't valid html:

from http://www.w3schools.com/tags/att_standard_id.asp

Specifies a unique id for an element. Naming rules:

Must begin with a letter A-Z or a-z Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".") Values are case-sensitive

tster
  • 17,883
  • 5
  • 53
  • 72
1

Square brackets are not legal in IDs. HTML data types says:

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 (".").

Also, to select and ID, you need to preface your selector with #, $('#foo').

michaeltwofish
  • 4,096
  • 3
  • 28
  • 32
  • It's not legal, but it's used a lot in forms so I can parse it through PHP and access as multidimensional array. The # is there in my original code -> just missed it in re-typing :-) – niggles Aug 27 '10 at 04:52
0
$('#foo[1]').val(); // you missed # which denotes id

on the other hand, $('#foo[1]').val(); will not work because brackets is for attributes e.g. $('#foo[name]') will get the element with id foo and with ann attribute name .

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • 1
    @tster: you'll need to escape: $('#foo\\\[1\\\]').val(); – Daniel Sloof Aug 27 '10 at 04:43
  • @Daniel, yes it will work, but I would not suggest it because it's not proper in this situation. id with `[` and `]` is not a valid. ;) – Reigel Gallarde Aug 27 '10 at 04:47
  • 1
    @Daniel, yes it will work. Still lets stick with the "write valid html first" line:) – tster Aug 27 '10 at 04:47
  • @Reigel, @tster: then don't make your main argument that it is used for jQuery specific functionality. That is not the issue here :) – Daniel Sloof Aug 27 '10 at 04:49
  • It's not **all** [invalid HTML](http://stackoverflow.com/questions/3577724/characters-not-allowed-in-dom-ids-by-spec-and-by-browser/3577773#3577773). We are in a transitional phase. – Anurag Aug 27 '10 at 05:59
0

First if you are going to get an item by id , you should add "#" before the id ,

$('#foo[1]').

Second the $() will return all elements that match the supplied pattern , so you will get all elements with that ID.

Mohamed Faramawi
  • 641
  • 5
  • 13
0

Using foo[1] doesn't really make it a multidimensional array, also you can't explicitly use the [ ] brackets like that, jQuery has a selector that uses that, so you need to use $('div[id="foo[2]"]') to get it to work, demo attached. I'd recommend just using foo1 though.

Demo http://jsfiddle.net/RSRSQ/

Robert
  • 21,110
  • 9
  • 55
  • 65