How do I properly load the a certain value into a textbox using jquery?Tried the one below but I get the [object Object]
as output. Please enlighten me on this, I'm new to jquery.
proc = function(x, y) {_x000D_
var str1 = $('#pid').value;_x000D_
var str2 = $('#qtytobuy').value;_x000D_
var str3 = $('#subtotal').load('compz.php?prodid=' + x + '&qbuys=' + y);_x000D_
$('#subtotal').val(str3);_x000D_
}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
_x000D_
_x000D_
<form name="yoh" method="get">_x000D_
Product id: <input type="text" name="pid" value=""><br/> _x000D_
Quantity to buy:<input type="text" name="qtytobuy" value="" onkeyup="proc(document.yoh.pid.value, this.value);"></br>_x000D_
_x000D_
Subtotal:<input type="text" name="subtotal" id="subtotal" value=""></br>_x000D_
<div id="compz"></div>_x000D_
_x000D_
</form>
_x000D_
This question is related to
javascript
jquery
try
subtotal.value= 5 // some value
proc = async function(x,y) {_x000D_
let url = "https://server.test-cors.org/server?id=346169&enable=true&status=200&credentials=false&methods=GET&" // some url working in snippet_x000D_
_x000D_
let r= await(await fetch(url+'&prodid=' + x + '&qbuys=' + y)).json(); // return json-object_x000D_
console.log(r);_x000D_
_x000D_
subtotal.value= r.length; // example value from json_x000D_
}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
_x000D_
<form name="yoh" method="get"> _x000D_
Product id: <input type="text" id="pid" value=""><br/>_x000D_
_x000D_
Quantity to buy:<input type="text" id="qtytobuy" value="" onkeyup="proc(pid.value, this.value);"></br>_x000D_
_x000D_
Subtotal:<input type="text" name="subtotal" id="subtotal" value=""></br>_x000D_
<div id="compz"></div>_x000D_
_x000D_
</form>
_x000D_
Note that the .value
attribute is a JavaScript feature. If you want to use jQuery, use:
$('#pid').val()
to get the value, and:
$('#pid').val('value')
to set it.
Regarding your second issue, I have never tried automatically setting the HTML value using the load
method. For sure, you can do something like this:
$('#subtotal').load( 'compz.php?prodid=' + x + '&qbuys=' + y, function(response){ $('#subtotal').val(response);
});
Note that the code above is untested.
I would like to point out to you that .val() also works with selects to select the current selected value.
Source: Stackoverflow.com