3

I have two jquery click functions that sets and retrieves value from a javascript object.

var poly;

$( document ).ready(function() {
    //save drawing
    $("#save").click( function () {
        poly = new Polygon();
        poly.savedrawingaspoly(map.polygonevent);
        poly.tract = tract;
        poly.name = $('#polyname').val();
        console.log(poly);
    });

    $("#btnClear").click(function () {
        console.log(poly);
        //poly.clearplot();
    });
});

the console.log(poly); in $("#save").click works. the console.log(poly); in $("#btnClear").click returns undefined. How do I spread the scope of the variable, so the second one can see the variable? Thanks.

7
  • Are you clicking btnClear before save ? Commented Aug 17, 2017 at 4:37
  • 1
    Gonna second this, is btnClear being clicked before save? Otherwise, this code should work... Commented Aug 17, 2017 at 4:41
  • Write this line in above top: var poly; poly = new Polygon(); Commented Aug 17, 2017 at 4:43
  • At the first line, you only declare a variable called poly without defining it (you do not assign a value to poly). So if you click btnClear before Save, you'll get undefined. Inside the Save click callback, you assign new Polygon() to poly so it works fine. Commented Aug 17, 2017 at 4:45
  • but if we assign then their problem solved Commented Aug 17, 2017 at 4:45

3 Answers 3

1

Use the window object to keep your variable in browser context. So the new code will look like:

$("#save").click( function () {
        poly = new Polygon();
        poly.savedrawingaspoly(map.polygonevent);
        poly.tract = tract;
        poly.name = $('#polyname').val();
        console.log(poly);

        window.poly = poly;
});

After implementing the code mentioned above; you can access poly variable through any function and also through your browser console.

Sign up to request clarification or add additional context in comments.

Comments

0

You can store and access the object at .data() of #save jQuery object

$( document ).ready(function() {
    //save drawing
    $("#save").click( function () {
        $(this).data().poly = new Polygon();
        $(this).data().poly.savedrawingaspoly(map.polygonevent);
        $(this).data().poly.tract = tract;
        $(this).data().poly.name = $('#polyname').val();
        console.log($(this).data().poly);
    });

    $("#btnClear").click(function () {
        if ($("#save").data().poly !== undefined) {
          $("#save").data().poly.clearplot();
        } else {
          console.log($("#save").data())
        }
    });
});

1 Comment

@Barmar Your initial comment did compel a re-read of Answer and alert to the fact that was missing $("#save").data() part of approach at $("#btnClear") handler.
0

Define like global and use it across the events and function

$( document ).ready(function() {
    //save drawing
    var poly = new Polygon();
    $("#save").click( function () {
        poly.savedrawingaspoly(map.polygonevent);
        poly.tract = tract;
        poly.name = $('#polyname').val();
        console.log(poly);
    });

    $("#btnClear").click(function () {
        console.log(poly);
        //poly.clearplot();
    });
});

Comments

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.