marry
marry

Reputation: 173

How to display a result passed to a function in a textbox

i have a function the retrieves data from a database and returns it to a function.i am trying to get the Income value from the result to be displayed in the text box.

this is what i tried.

function Display(result) // result gets passed
{
 var income= $('#Income'); //getting the textbox;
income.value=result.Income  // when i try this result.Income,Income comes back as undefined.
 }

How do i display the value from my result into the textbox? this is how my "result" looks thats passed into the Display function.So ideally the value for Income is "Salary" so salary should be displayed in the textbox.

{
ID: 0
Number: 520
OtherIncome: "Private"
Income: "Salary"
}
This is my HTML
<input type="text" name="Income" id="Income" class="text ui-widget-content ui-corner-all" value="">

Upvotes: 0

Views: 1378

Answers (3)

Limitless Claver
Limitless Claver

Reputation: 497

Let's explain your code


var json = {
  ID: 0,
  Number: 520,
  OtherIncome: "Private",
  Income: "Salary",
}

function Display(result) // result gets passed
{
  var income = $('#Income'); //getting the textbox;
  income.val(result.Income) // when i try this result.Income,Income comes back as undefined.
}

you have an object of data stored in a variable json. Then a function Display(param) that takes a parameter. Then in your function, you were trying to access the empty parameter. The parameter result is a variable and it is not defined. So actually it has a value of undefined. hence why you get undefined when you invoke display without passing in any parameters. Assume you pass in a valid parameter in your function invoking then you would get the right result.

Example:


var json = {
  ID: 0,
  Number: 520,
  OtherIncome: "Private",
  Income: "Salary",
}


//invoke you function with the json data
Display(json)
//this should work perfectly.


//you can even remove the function and just make it

var income = $('#Income'); 
 income.val(json.Income);

//But you only do this if you want to get the value once



Upvotes: 0

Sinha
Sinha

Reputation: 512

var json = {
  ID: 0,
  Number: 520,
  OtherIncome: "Private",
  Income: "Salary",
}

function Display(result) // result gets passed
{
  var income = $('#Income'); //getting the textbox;
  income.val(result.Income) // when i try this result.Income,Income comes back as undefined.
}
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="Income" id="Income" class="text ui-widget-content ui-corner-all" value="">
<button onClick="Display(json)">Click</button><br>
Click the button to set the textbox value.

Try using income.val(result.Income) to set the value of the text box. Here I have used a button click event to generate the example.

Upvotes: 1

David Machado
David Machado

Reputation: 430

function Display(result) {
  var income = $('#Income'); //getting the textbox;
  //income.value=result.Income  here use the .val() method
  income.val(result.Income);
}

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

Upvotes: 1

Related Questions