简体   繁体   中英

Javascript onclick function html

I am trying to customize some public git, and because I am a noob in Jquery/Javascript I do not know how to properly bind an onclick function to the button, so that it will know how to call getStates() function. Yes I know that I could just remove $(function(){ and it would work, but I want to be able to call it within jquery function ?

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>US Hospitals</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v2.2.3/mapbox.js'>
</script>
<link href='https://api.mapbox.com/mapbox.js/v2.2.3/mapbox.css' rel='stylesheet' />
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="/lib/neo4j-web.min.js"></script>
</head>
<body>
<div id="desc">
    <div class="row top-row">
         <div class="col-xs-6">
            <h3 class="heading">US Hospitals</h3>
        </div>
        <div class="col-xs-6">
            <p class="lead"></p>
        </div>
        <button onclick="$.getStates();">Load States!</button>
    </div>
</div>
<div id='map'></div>
<script>
 $(function(){

    function getStates() {    
        session
            .run(districtQuery)
            .then(function(result){
                result.records.forEach(function(record) {
                     drawPolygons(parseWKTPolygons(record.get("wkt")), 
record.get("text"));

                });
            })
    }
</script>
</body>
</html>

I get the error saying :

Uncaught TypeError: $.getStates is not a function at HTMLButtonElement.onclick (index.html:51)

$(function () { })

This is telling the interpreter to run any JavaScript/jQuery code as soon as the page is ready. This are usually referred to as an onready function. You don't typically create other functions inside of here.

Remove:

$(function () { })

Also, the way you are creating the onclick event is wrong. Use getStates() instead of $.getStates()

So all in all, your code should be changed as following:

<button onclick="getStates();">Load States!</button>

<script>
function getStates() {    
    session
        .run(districtQuery)
        .then(function(result){
            result.records.forEach(function(record) 
            {
                 drawPolygons(parseWKTPolygons(record.get("wkt")), 
                 record.get("text"));

            });
        });
}
</script>

Another way you could fix it is to leave your onready function and create the click event inside. Your getStates() function still needs to be placed outside of the onready function though and you would remove the onclick from the button element.

For example:

<button>Load States!</button>

<script>
$(function() {
    $('button').click(function () {
        getStates();
    });
});

function getStates() {    
    session
        .run(districtQuery)
        .then(function(result){
            result.records.forEach(function(record) {
                 drawPolygons(parseWKTPolygons(record.get("wkt")), 
                 record.get("text"));

            });
        })
}
</script>

Now if you have more than one button on this page, you will need to add an ID or class to distinguish them. Then you'd replace $('button') with for example: $('#load_states_button) if using an ID or $('.button_class') if using a class.

These all produce the same result, so how to do it is just a matter of personal preference.

Hope this helped! Let me know if you have any questions. :)

You can make the function global:

window.getStates = function getStates() {
    ...
};

Then you can simply use getStates() in the onclick attribute.

Of course, the cleaner way to do this would be to bind the event from JavaScript itself, using jQuery's $.click() . This would require you to add an id attribute to the button tag, or some other way to identify it from jQuery.

Take the function out of $(function() { ... }) , because functions called from onclick have to be in the global scope. And call it as getStates() , not $.getStates()

<script>
function getStates() {    
    session
        .run(districtQuery)
        .then(function(result){
            result.records.forEach(function(record) {
                drawPolygons(parseWKTPolygons(record.get("wkt")), 
                record.get("text"));
            });
        })
}
</script>

You could add an id to the button and use something like this:

<button id="load-states">Load States!</button>

Javascript:

 $(function() {
   function getStates() {
    session
        .run(districtQuery)
        .then(function(result){
            result.records.forEach(function(record) {
                 drawPolygons(parseWKTPolygons(record.get("wkt")),record.get("text"));
            });
        })
   }
// Adding event onClick to button
$('#load-states').on('click', getStates);                            

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM