0
<input type="text" name="fruits[]" value="Apple">
<input type="text" name="fruits[]" value="Banana">
<input type="text" name="fruits[]" value="Orange">

My question is how to change the value of "Orange" to "Grapes" using jquery? The below code is not working.

<script>
    $("input[name='fruits[2]']").val("Grapes"); 
</script>

Thanks in advance.

3 Answers 3

1

Try using eq(index):

$(function(){ //<-- Add this in DOM ready wrapper as well
   $("input[name='fruits[]']").eq(2).val("Grapes"); 
});

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1
  1. The code needs to be executed in dom ready handler
  2. your selector input[name='fruits[2]'] looks for input elements with name fruits[2] instead of with name fruits[] and is at the 3rd index

So

jQuery(function () {
    $("input[name='fruits[]']:eq(2)").val("Grapes");
})

Demo: Fiddle

Comments

0

Try this:

$("input[value='Orange']").val('Grapes');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.