简体   繁体   中英

How to pass a variable to a parameter (in a function)?

I'm very new to JS and jquery, and I tried to find a direct answer but to no avail (most likely I'm googling the wrong questions).

I want to pass the variables x, y and d as parameters in function solution.

What am I doing wrong?


$('#form-send').click( function(){
    event.preventDefault();

    var x = $('#x-input').val();
    var y = $('#y-input').val();
    var d = $('#d-input').val();
    console.log(x,y,d);

    function solution(x,y,d){
        console.log(x,y,d);
    }
});

You need to do this

function solution(x, y, d) {
  console.log(x, y, d);
}

$('#form-send').click(function(e) {
  e.preventDefault();

  var x = $('#x-input').val();
  var y = $('#y-input').val();
  var d = $('#d-input').val();

  solution(x, y, d)
});
//function definition 
function solution(x, y, d) {
    console.log(x, y, d);
}

$('#form-send').click(function() {
    event.preventDefault();

    var x = $('#x-input').val();
    var y = $('#y-input').val();
    var d = $('#d-input').val();
    solution(x, y, d); //Function call   
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM