We help IT Professionals succeed at work.

jQuery From Input Update

Robert Granlund
on
I have a form that I would like to change the value of an input by it's name.  For some reason I'm having an issue with targeting the name:

<script>
$( 'input[name="item[0][total"]' ).val( damage_total );
</script>

<html>
<input type="text" value="296" name="item[0][total]">
</html>

Open in new window

Comment
Watch Question

John EastonDirector

Commented:
I'm not great at jquery - still using old fashion javascript.  But it looks like your apostrophes' are out of place.  I think your line 2 should read:
$( 'input[name="item[0][total]"' ).val( damage_total );

Open in new window

I hope it is that simple.
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
You need to escape the array brackets:

$('input[name=item\\[0\\]\\[total\\]]' ).val( damage_total );

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Actually, scratch that:

JEaston was sort of right, although his answer missed a closing bracket.

This is what you need:

$('input[name="item[0][total]"]' ).val( damage_total );
Most Valuable Expert 2013
Awarded 2012

Commented:
Hi,
I guess just a simple typo - there was one closing bracket missing:
$( 'input[name="item[0][total]"]' ).val( damage_total );

Open in new window

But you should reconsider to change your element names as I doubt that brackets are allowed characters.

HTH
Rainer
Most Valuable Expert 2013
Awarded 2012

Commented:
Too late :-) By 16 seconds :-]
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
@Rainer - brackets are allowed in inputs and actually I'd strongly recommend them - they're an easy way to group form data - making the POST processing much simpler