jQuery From Input Update

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:

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

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

Open in new window

Who is Participating?
Chris StanyonCommented:
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 );
John EastonDirectorCommented:
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.
Chris StanyonCommented:
You need to escape the array brackets:

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

Open in new window

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rainer JeschorCommented:
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.

Rainer JeschorCommented:
Too late :-) By 16 seconds :-]
Chris StanyonCommented:
@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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.