0

I'm trying to set a default value for a function parameter, that will query my server for an initial value.

The end goal is i will be able to click on one of the list items I create to retrieve and create a new list.

<script type="text/javascript"> $(function getJobs(jobid=0) { {#jobid = 0;#} console.log("jobid: " + jobid); let query = {id: jobid}; console.log(query); $.getJSON("{% url 'website:get-jobs' %}", query, function (data) { console.log("getjson"); $.each(data, function (key, value) { console.log(key + " - " + value); $('#jobs-list').append("<li id='" + key + "'" + "href='#'" + "onclick=getJobs(key)" + ">" + value + "</li>"); }); }); }) </script> 

If I manually set jobid to 0(commented out in the code above) everything works perfectly. If i try to set it in the function parameter list i get this in my console.log: jobid: function(e,t){return new w.fn.init(e,t)}

3
  • Where is the code that calls this? The jQuery ready syntax (ie $(function() { ... })) passes the jQuery global as the sole function argument, ie jQuery(function(jQuery) { ... }). See api.jquery.com/jquery/#jQuery3
    – Phil
    CommentedApr 8, 2019 at 4:11
  • I highly doubt you actually have code that calls this so I'm really not sure what you're trying to do here. When would jobid ever not be 0?
    – Phil
    CommentedApr 8, 2019 at 4:12
  • The list items are populated with a key from a list of jobs. Initially the key is 0 to get all parent jobs(id=0) after that every time you click a list item it will retrieve a new list of jobs that have that parent id and clear and populate the list again with the new data.
    – IAMCB
    CommentedApr 8, 2019 at 23:37

1 Answer 1

1

When jQuery calls the callback to $(function(x) { ... }), the argument is the jQuery object itself, therefore having a default value is never going to be needed

so, you'll want to do this instead

$(function() { function getJobs(jobid = 0) { console.log("jobid: " + jobid); let query = {id: jobid}; console.log(query); $.getJSON("{% url 'website:get-jobs' %}", query, function (data) { console.log("getjson"); $.each(data, function (key, value) { console.log(key + " - " + value); $('#jobs-list').append("<li id='" + key + "'" + "href='#'" + "onclick=getJobs(key)" + ">" + value + "</li>"); }); }); } getJobs(); }); 

alternatively, using your original code, you could of course test for the case where typeof jobid is a function, and set it to 0 instead ... i.e.

$(function getJobs(jobid) { if (typeof jobid === 'function') { jobid = 0; } console.log("jobid: " + jobid); let query = {id: jobid}; console.log(query); $.getJSON("{% url 'website:get-jobs' %}", query, function (data) { console.log("getjson"); $.each(data, function (key, value) { console.log(key + " - " + value); $('#jobs-list').append("<li id='" + key + "'" + "href='#'" + "onclick=getJobs(key)" + ">" + value + "</li>"); }); }); }); 
0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.