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:

<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

LVL 7
rgranlundAsked:
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 );
0
 
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.
0
 
Chris StanyonCommented:
You need to escape the array brackets:

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

Open in new window

0
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:
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
0
 
Rainer JeschorCommented:
Too late :-) By 16 seconds :-]
0
 
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
0
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.