0

I have a method that creates 2 arrays with data. I want to be able to include these arrays into JSON. Here is my code:

$(document).ready(function() {
    $("button").click(function(){
        var configs = [];
        var testPages = [];
        $(".test input:checked").each(function() {
            testPages.push($(this).attr('id'));
        });
        $(".config input:checked").each(function() {
            configs.push($(this).attr('id'));
        });
        //console.log(configs);
        //console.log(testPages);

        var testJson = new Object();
        testJson.testpages = testPages;
        testJson.configurations = configs;
        var runTestJson = JSON.stringify(testJson);
        return runTestJson;
    });
});

I want to be able to test this against my page but I am new to jQuery, Javascript and also using Chrome developer tools. I'm not sure how to test this in the Chrome console.

The commented portion (console.log()s) were displayed but when I type configs or testPages to try to view the array, I would get the error that they were undefined. How do I test to see if I am getting the correct output for my code on Chrome developer tools?

0

2 Answers 2

2

You'd need to make those two variables global. Just beware that unnecessarily polluting the global scope is not recommended. But if it's just for testing, then i suppose it's OK.

// global scope
var configs = [];
var testPages = [];
$(document).ready(function() {
    $("button").click(function(){
        $(".test input:checked").each(function() {
            testPages.push($(this).attr('id'));
        });
        $(".config input:checked").each(function() {
            configs.push($(this).attr('id'));
        });
        //console.log(configs);
        //console.log(testPages);

        var testJson = new Object();
        testJson.testpages = testPages;
        testJson.configurations = configs;
        var runTestJson = JSON.stringify(testJson);
        return runTestJson;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can debug your JavaScript without populating global space as follows (Instruction based on chrome, but similar in major browsers):

  • Open developer console F12
  • go to Sources tab
  • Open your script file (Ctrl + o and type the filename)
  • Put a breakpoint by clicking on the line number you wish to debug
  • Refresh your page an execution will be stopped at that line

. You can further use the available controls in the debug toolbar: enter image description here

to proceed the execution as you wish, each controls have self-descriptive tooltips. You can see the values in variables by hovering over them or manually selecting them.

3 Comments

When you mean populating global space, do you mean putting variables in the global scope? I am also curious as to if all the the JS/jQ code is required to be within $(document).ready(function() {}). I read somewhere that code should be within that function so that the code is loaded before the rest of the page.
@Liondancer Globalspace meaning the window object. If you simply create a variable outside a function or omit the var keyword, the variable will be a property of the window object which you can access like window.variablename.
... .ready() method is prefered to make sure that the DOM is created before your script runs, ensuring all the events handlers, styles etc are properly attached... If part of your script doesn't interfere with DOM then it doesn't necessarily have to be in ready()

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.