115

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) {
  var str1 = $('#pid').value;
  var str2 = $('#qtytobuy').value;
  var str3 = $('#subtotal').load('compz.php?prodid=' + x + '&qbuys=' + y);
  $('#subtotal').val(str3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<form name="yoh" method="get">
  Product id: <input type="text" name="pid" value=""><br/> 
  Quantity to buy:<input type="text" name="qtytobuy" value="" onkeyup="proc(document.yoh.pid.value, this.value);"></br>

  Subtotal:<input type="text" name="subtotal" id="subtotal" value=""></br>
  <div id="compz"></div>

</form>
Ym90
  • 31
  • 6
Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
  • what is str3? Where is it declared? It seems that you are trying to load an object as value when you probably just need a property from that object- – Fgblanch Nov 16 '10 at 12:07
  • sorry, forgot to set st3 = $('#subtotal').load('compz.php?prodid=' + x + '&qbuys=' + y); – Wern Ancheta Nov 16 '10 at 12:11

4 Answers4

181

I think you want to set the response of the call to the URL 'compz.php?prodid=' + x + '&qbuys=' + y as value of the textbox right? If so, you have to do something like:

$.get('compz.php?prodid=' + x + '&qbuys=' + y, function(data) {
    $('#subtotal').val(data);
});

Reference: get()

You have two errors in your code:

  • load() puts the HTML returned from the Ajax into the specified element:

    Load data from the server and place the returned HTML into the matched element.

    You cannot set the value of a textbox with that method.

  • $(selector).load() returns the a jQuery object. By default an object is converted to [object Object] when treated as string.

Further clarification:

Assuming your URL returns 5.

If your HTML looks like:

<div id="foo"></div>

then the result of

$('#foo').load('/your/url');

will be

<div id="foo">5</div>

But in your code, you have an input element. Theoretically (it is not valid HTML and does not work as you noticed), an equivalent call would result in

<input id="foo">5</input>

But you actually need

<input id="foo" value="5" />

Therefore, you cannot use load(). You have to use another method, get the response and set it as value yourself.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • does it really get more difficult if I just want to change where the result is being loaded? My code above is functional when I load it into a div. – Wern Ancheta Nov 16 '10 at 12:22
  • 1
    @Ieyasu Sawada: Well, currently you are passing a JavaScript object to `$('#subtotal').val(str3);` because `load()` does not work as you thought it works. Yes, you can use `load()` to load text or HTML into a `div` but not as textbox value. – Felix Kling Nov 16 '10 at 12:25
  • @Ieyasu Sawada: See my update for further clarification, hope it helps! – Felix Kling Nov 16 '10 at 12:33
  • one last question?Why do I get spaces before the actual result on the output?Is it possible to strip them in jquery,basically compz.php contains lots of whitespaces for better readability and those whitespace are actually being outputted together with the result – Wern Ancheta Nov 16 '10 at 12:37
  • @Ieyasu Sawada: You can use normal JavaScript functions, e.g. with reglular expression: `data = data.replace(/\s+/, '');` – Felix Kling Nov 16 '10 at 12:50
  • @Kyokasuigetsu Or you can just use $.strip(data) – JamieJag Feb 22 '13 at 12:58
58

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.

http://api.jquery.com/val/

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.

http://api.jquery.com/load/

chash
  • 3,975
  • 13
  • 29
Mauro
  • 2,032
  • 3
  • 25
  • 47
  • 2
    It's worth pointing out the relevant [jQuery api](http://api.jquery.com/) page for [`val()`](http://api.jquery.com/val/), for reference purposes. – David Thomas Nov 16 '10 at 12:09
  • 1
    Even if is way too basic to point it out, maybe? Just my humble opinion... Anyhow updated! – Mauro Nov 16 '10 at 12:13
  • compz.php computes the product of the 2 numbers in the input. I guess the part which loads the computed values: $('#subtotal').load('compz.php?prodid=' + x + '&qbuys=' + y); is being treated as object that's why I see object object as output, how then do I convert it to a normal value – Wern Ancheta Nov 16 '10 at 12:18
  • 1
    It's never **too basic**, look at the question again. If he'd seen the page, or known its content, he probably wouldn't have asked the question =) – David Thomas Nov 16 '10 at 12:19
  • This does not help the OP. He uses `val()` correctly, just the value he passes to it is not correct. – Felix Kling Nov 16 '10 at 12:23
  • 1
    Pointing out the real problem would have been a good starting point Peace and Love ;) – Mauro Nov 16 '10 at 12:30
5

I would like to point out to you that .val() also works with selects to select the current selected value.

Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36
0

try

subtotal.value= 5 // some value

proc = async function(x,y) {
  let url = "https://server.test-cors.org/server?id=346169&enable=true&status=200&credentials=false&methods=GET&" // some url working in snippet
  
  let r= await(await fetch(url+'&prodid=' + x + '&qbuys=' + y)).json(); // return json-object
  console.log(r);
  
  subtotal.value= r.length; // example value from json
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form name="yoh" method="get"> 
Product id: <input type="text" id="pid"  value=""><br/>

Quantity to buy:<input type="text" id="qtytobuy"  value="" onkeyup="proc(pid.value, this.value);"></br>

Subtotal:<input type="text" name="subtotal" id="subtotal"  value=""></br>
<div id="compz"></div>

</form>
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345