Shams Nahid
Shams Nahid

Reputation: 6559

How to get a value in the jQuery, that is sent from the node js server?

In the node.js server, I sent a value in json format and need to put that in a javascript variable. I send the value like

res.render('/path/', {
    testVal: 'testVal'
});

In the handlebars I can get it by

{{testVal}}

Now I want to get it in a javascript variable in the client side. Like in jQuery

window.onload = function() {
        if (window.jQuery) {
            // jQuery is loaded
            var test = testVal;
            alert(test);
        }
    }

How to get the testVal in the test variable?

Upvotes: 3

Views: 264

Answers (1)

Ryad Boubaker
Ryad Boubaker

Reputation: 1501

Assuming that you are using jquery in the same file the data is rendered then you can do something like this :

<script>
window.onload = function() {
    if (window.jQuery) {
        // jQuery is loaded
        var test = "{{ testVal }}";
        alert(test);
    }
}
</script>

Upvotes: 1

Related Questions