I have an input field, input type text:
<input type="text" id="show-val" value="">
<button id="get-val">Val</button>
On button click it's taking a URL from somewhere else, I need to place the value into the text field's value attribute.
What I could is:
$('#get-val').on('click', function(){
var url = 'http://example.com/'; //suppose this is our grabbed URL
$('#show-val').val( url );
});
But if you inspect the input text then can see it's not writing the url into the value attribute, it's just pasting the value into the text field.
What I tried:
$('#get-val').on('click', function(){
var url = 'http://example.com/'; //suppose this is our grabbed URL
$('#show-val').prop( 'value', url ); //failed
$('#show-val').prop( 'placeholder', url ); //passed
});
Output is:
<input type="text" id="show-val" value placeholder="http://example.com/">
Why am I failing writing text field's value attribute? How can I write actually?
$('#show-val').val( url );is correct, However if you want to modify the value attribute the use$('#show-val').attr( 'value', url );