user3802409
user3802409

Reputation:

javascript - checkbox onclick function in another function

I'm building a web site with javascript. I have a leaflet map and some markers on it. I have a list and checkboxes on a div. I want to filter markers with checkboxes. But my onclick function can not reach marker's data. like this:

function onclick() {
    //delete marker
    //i can not reach marker's data from here
}

function initializeMap() {
    //something
    function markers() {
        //my markers defined here
        function onclickIwant() {
            //something
        }
    }
}

I want to use onclickIwant function for checkbox onclick or reach marker's data from onclick function. How can I do?

my html:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    <link rel="stylesheet" href="leaflet.css"/>
  </head>
    <body>
        <div id="harita"></div>
        <script src="leaflet.js"></script>
        <script src="index.js"></script>
        <div id="liste">
            <input type="checkbox" id="id1" checked="true" onclick="somefunction();"><label>id1</label>
            <input type="checkbox" id="id2" checked="true" onclick="somefunction();"><label>id2</label>
            <input type="checkbox" id="id3" checked="true" onclick="somefunction();"><label>id3</label>
            <ol id="Listesi"></ol>
        </div>
    </body>
</html>

Upvotes: 0

Views: 214

Answers (1)

benjasHu
benjasHu

Reputation: 354

I think you could something like this:

function onclick() {
    //delete marker
    //i can not reach marker's data from here
    window.onclickIwant();
}

function initializeMap() {
    //something
    function markers() {
        //my markers defined here
        window.onclickIwant = function() {
            //something
        }
    }
}

Registering onclickIwant function into window object, you can use it anywhere.

Good luck!

Upvotes: 1

Related Questions