Good evening) What am I doing wrong?

function myFunction() { var name = $("name").val(); var cost = $("cost").val(); var comment = $("comment").val(); var tmp = "site.ru/?x" + name + "&z=" + cost + "&y=" + comment; var result = encodeURI(tmp); $("textarea#tar1").html(result); console.log(result); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="name" name="input1"> <input type="text" id="cost" name="input2"> <input type="text" id="comment" name="input3"> <textarea id="tar1" rows="9"></textarea> <button onclick="myFunction()">Try it</button> 

I get the result instead of links with variables:

 site.ru/?xundefined&z=undefined&y=undefined 

    1 answer 1

    Forgot to place in the selectors a sign denoting id ( # ).

     function myFunction() { var name = $("#name").val(); var cost = $("#cost").val(); var comment = $("#comment").val(); var tmp = "site.ru/?x" + name + "&z=" + cost + "&y=" + comment; var result = encodeURI(tmp); $("textarea#tar1").html(result); console.log(result); } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="name" name="input1"> <input type="text" id="cost" name="input2"> <input type="text" id="comment" name="input3"> <textarea id="tar1" rows="9"></textarea> <button onclick="myFunction()">Try it</button> 

    • Thank you old man! Good evening! - Anton Bogomolov