MGames
MGames

Reputation: 1181

Jquery - Input name value not changing but id is?

I'm trying to change the value of this HTML code by using input name:

<input class="string required" first_and_last="true" id="high_billing_name" name="high[billing_name]" required="required" size="20" type="text">

When I use the Input id to change the value it works:

$("#high_billing_name").val('NAME');

But I'm trying to change the value using the name instead of the id. I've tried $('input[name="high[billing_name]"]').val("NAME");and other forms of that but it doesn't work.

Upvotes: 0

Views: 151

Answers (3)

Teocci
Teocci

Reputation: 8885

This example is using JQuery, it is straight forward:

$('input[name="high[billing_name]"]').val("Johnny Bravo");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="string required" first_and_last="true" id="high_billing_name" name="high[billing_name]" required="required" size="20" type="text">

Also, this version using JQuery, but I use \ to print the ":

$('input[name=\"high[billing_name]\"]').val("Johnny Bravo");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="string required" first_and_last="true" id="high_billing_name" name="high[billing_name]" required="required" size="20" type="text">

But this one is using pure javascript:

var imput = document.getElementById("high_billing_name");
imput.value = "Johnny Bravo";
<input class="string required" first_and_last="true" id="high_billing_name" name="high[billing_name]" required="required" size="20" type="text">

Upvotes: 0

becquerel
becquerel

Reputation: 1131

Maybe it depends on the jquery version or even browser version? The syntax you use should work.

$('input[name="high[billing_name]"]').val("NAME")

I created a simple fiddle for it and this one works for me in Chrome 54.

https://jsfiddle.net/mpdozfqc/

Upvotes: 0

Dhaval Soni
Dhaval Soni

Reputation: 305

Try this. $('input[name=\"high[billing_name]\"]').val("NAME");

Upvotes: 1

Related Questions