lokeshjain2008 lokeshjain2008 - 4 months ago 14
Javascript Question

Select dom elements by attributes having array like attributes

I have many elements that are structured to get them in array like mapping on server side.

<input type="CHECKBOX" id="478" value="1" name="data[GroupInfo][student][478]" onclick="return updateValues('478')">
<input type="CHECKBOX" id="490" value="1" name="data[GroupInfo][student][490]" onclick="return updateValues('490')">

<input type="CHECKBOX" id="478" value="1" name="data[ClassInfo][student][478]" onclick="return updateValues('478')">
<input type="CHECKBOX" id="490" value="1" name="data[ClassInfo][student][490]" onclick="return updateValues('490')">


so on...
Now, I want to select them using their name attribute like

$("[name^=data[ClassInfo][student]]");


but this won't work
I tried to escape barckets to.

$("[name^=data\[ClassInfo\]\[student\]]");


but no luck;

I want to select them using name attribute.

Answer

Just wrap the attribute value in ""

$('input[name^="data[ClassInfo][student]"]')

Demo: Fiddle

Comments