0

This is my html:

<div id="final_total_amt"></div>

This is my js:

var final_item_total = 'Test!';
$('#final_total_amt').text(function(final_item_total) {
 return final_item_total;
});

I know this code works,

$('#final_total_amt').text(final_item_total);  // Out: Test! 

But i want to show 'Test!' the jQuery code with the function(final_item_total)

3 Answers 3

1

You defining a function in your text call. You can't do that. Define your function first:

function final_item_total_function() {
  var final_item_total = 'Test!';
  return final_item_total;
}
$('#final_total_amt').text(final_item_total_function);
Sign up to request clarification or add additional context in comments.

Comments

1

If you return a parameter, the function will only print the parameter value. In your case, this is undefined because you are not passing any parameter to the function when you are executing it.

To return a diferent variable you can just return it.

Check the following example:

var final_item_total = 'Test!';

$('#final_total_amt').text(() => {
 return final_item_total;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="final_total_amt"></div>

You can also write it like this:

var final_item_total = 'Test!';

$('#final_total_amt').text(function() {
 return final_item_total;
});

Comments

0

This is my solution:

var final_item_total = 'Test!';
var result=getResult(final_item_total);
$('#final_total_amt').text(result);
function getResult(z){
	return z;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="final_total_amt"></div>

Comments

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.