204

I can't find the right selector for:

<input maxlength="6" size="6" id="colorpickerField1" name="sitebg" value="#EEEEEE" type="text">

I want to change the value to = 000000. I need the selector to find the "name" not the id of the text input.

Shouldn't this work?:

$("text.sitebg").val("000000");

The presented solution does not work, what's the problem with this?

$.getJSON("http://www.mysitehere.org/wp-content/themes/ctr-theme/update_genform.php",function(data) {
    $("#form1").append(data.sitebg);
    $('input.sitebg').val('000000');
});

The JSON data is working correctly; the idea is to later pass the JSON values into the form input text values. But is not working :(

6 Answers 6

403

no, you need to do something like:

$('input.sitebg').val('000000');

but you should really be using unique IDs if you can.

You can also get more specific, such as:

$('input[type=text].sitebg').val('000000');

EDIT:

do this to find your input based on the name attribute:

$('input[name=sitebg]').val('000000');
Sign up to request clarification or add additional context in comments.

1 Comment

Why not use id, it's simple and understandable
53

jQuery way to change value on text input field is:

$('#colorpickerField1').attr('value', '#000000')

this will change attribute value for the DOM element with ID #colorpickerField1 from #EEEEEE to #000000

2 Comments

hi, is this working for you on chrome console?
This's the perfect answer when you have any DOM element for change value or any property you want.
34

Best practice is using the identify directly:

$('#id').val('xxx');

3 Comments

simple and best one :)
Why does this work but document.getElementById("#id").value = 'xxx'; does not work for me? It does not change the text value that appears, only the DOM element's value property.
You already use ById method, # is for css notation. Remove it.
21

When set the new value of element, you need call trigger change.

$('element').val(newValue).trigger('change');

1 Comment

This fixed a problem for me where the change event handler was getting called twice; once with the new value and then again with [attr=val]
3

Just adding to Jason's answer, the . selector is only for classes. If you want to select something other than the element, id, or class, you need to wrap it in square brackets.

e.g.

$('element[attr=val]')

Comments

0

jQuery change input text value using parent class

$('.form-submit input.submit').val('Johnny Bravo');
  • .form-submit is the parent class
  • Johnny Bravo is the text that you want to change

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.