1

With span and div this works

jQuery

$('#exchange_rate1').html(data[0].FinalCurrencyRate);

HTML

<span id="exchange_rate1"></span>

But if the HTML is an input element such as <input type='text' name='exchange_rate1' id='exchange_rate1' value='<?php echo $post_exchange_rate; ?>> then nothing happens.

Removed php code from value the same nothing.

I also tried document.getElementById("exchange_rate1").html(data[0].FinalCurrencyRate); but I also see nothing.

Now clear, that need to use val. I just searched google for how to insert jquery variable in input field. Could not find.

6 Answers 6

8

Use jQueryObject.val(some_value) to set the value of an input, not html().

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

1 Comment

But why is that?!?! (teach a man to fish...) IMHO, SO should be more than a "Ask question"..."get answer"; too many people just post the solution without actually explaining why the OP should do it that way, which is just going to result in another question down the road for the same or similar problem. @ 250k rep, you should know that.
2

To be more specific:

// store the value you're looking to assign
var data = [
  { FinalCurrencyRate: <?= $post_echange_rate; ?> }
];

the jQuery way:

$('#exchange_rate1')               // grab the <input>
  .val(data[0].FinalCurrencyRate); // and assign it from the variable

the straight js way:

// normal JS version:
document.getElementById('exchange_rate1') // grab the <input>
  .value = data[0].FinalCurrencyRate;     // assign it

Any kind of form fields (<input>,<select>,<textarea>) use .val() to get/set since they don't contain child elements. .html() should be used for structural elements.

Comments

1

In case of text use as

$("#exchange_rate1").val('Hello');

Comments

1

This is because you aren't supposed to be setting the innerHTML of the input element, but the value.

In jQuery you use the .val() method:

$('#exchange_rate1').val(data[0].FinalCurrencyRate);

Or with plain JavaScript, you're changing the value property of the HTMLInputElement object:

document.getElementById('exchange_rate1').value = data[0].FinalCurrencyRate;

Comments

1

For textual input boxes, use .val(), for textareas, use .text(), and for non-input type elements, use .html().

Comments

1

All you need is

$("#exchange_rate1").val('Hello');

1 Comment

And if u want to fetch the data then $("#exchange_rate1").val();

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.