Ukuser32
Ukuser32

Reputation: 2189

Selecting a named element in an Input array

I am running the following Jquery:

<input type="text" value="hello" name="multy['helloworld']" />
<script type="text/javascript">
  $(document).ready(function(){
    var hello = 'helloworld';
    alert($('input[name="multy['+hello+']"]').val());
  })
</script>

As you'll see it can't select the element by a named Array key. Is there a work around for this?

Ta.

Antony

Upvotes: 1

Views: 45

Answers (2)

gpayne_007
gpayne_007

Reputation: 579

You have to do double escapes on the brackets when you send it to jquery so jquery will know to escape them, too. Plus, please notice that I removed the single quote from the input in your example.

<input type="text" value="hello" name="multy[helloworld]" />
<script type="text/javascript">
    alert($('input[name=multy\\[helloworld\\]]').val());
</script>

Upvotes: 0

VisioN
VisioN

Reputation: 145388

Yes, remove the single quotes from the name of the element:

<input type="text" value="hello" name="multy[helloworld]" />

They are absolutely not required.

Upvotes: 1

Related Questions