$(function () {
var info = JSON.parse($('#userInfo').text());
var empFirstName = info.EmployeedFirstName;
var empLastName = info.EmployeedLastName;
var empFullName = empFirstName + " " + empLastName;
});
I am calling the summary function from a hyper link. I only want to call the summary function when the link is clicked on.
<a onclick="summary();">Summary</a>
function summary(empFullName){
}
Remove the inline function call from the link and add a click event handler to your link, then call the function and pass your variable to it, when the click handler gets called. Take a look at the snippet and you will get the idea.
$(function () { //var info = JSON.parse($('#userInfo').text()); //var empFirstName = info.EmployeedFirstName; //var empLastName = info.EmployeedLastName; var empFullName = 'foo' + " " + 'bar'; // added click event handler here $('a').on('click', function() { summary(empFullName); // call summary with empFullName }); }); function summary(empFullName){ console.log(empFullName); $('#output').text(empFullName); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a href="#">Summary</a> <div id="output"></div>
This will work for you, but you should look into the window
object, global variables, and why they may be considered bad.
$(function () {
var info = JSON.parse($('#userInfo').text());
var empFirstName = info.EmployeedFirstName;
var empLastName = info.EmployeedLastName;
window.empFullName = empFirstName + " " + empLastName;
});
function summary(empFullName){
// use empFullName here
}
Then modify your link to pass in the global
<a onclick="summary(empFullName);">Summary</a>
Some related reading: Should I use window.variable or var?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.