I'm trying to call getPointOnMap
function on the onclick event and to give it an json object as a parameter.
here is code samples:
$.ajax({
type: "POST",
url: "/getResult.json",
success: function(result) {
var html = '';
for (var i = 0; i < result.length; i++) {
var obj = result[i];
html += "<input type='checkbox' onClick='getPointOnMap(" + obj + ")'/>" + obj.address + "<br>";
}
$("#myDiv").append(html);
}
});
here is function getPointOnMap
function getPointOnMap(object) {
map.addMarker({
lat: object.lattitude,
lng: object.longtitude,
click: function(e) {
alert('You clicked in this marker');
}
});
}
firebug output(also in question name):
SyntaxError: missing ] after element list
getPointOnMap([object Object])
what should I do to pass correct object?
I'm trying to call getPointOnMap
function on the onclick event and to give it an json object as a parameter.
here is code samples:
$.ajax({
type: "POST",
url: "/getResult.json",
success: function(result) {
var html = '';
for (var i = 0; i < result.length; i++) {
var obj = result[i];
html += "<input type='checkbox' onClick='getPointOnMap(" + obj + ")'/>" + obj.address + "<br>";
}
$("#myDiv").append(html);
}
});
here is function getPointOnMap
function getPointOnMap(object) {
map.addMarker({
lat: object.lattitude,
lng: object.longtitude,
click: function(e) {
alert('You clicked in this marker');
}
});
}
firebug output(also in question name):
SyntaxError: missing ] after element list
getPointOnMap([object Object])
what should I do to pass correct object?
Share Improve this question edited Dec 1, 2013 at 1:45 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked May 29, 2013 at 12:36 John SmithJohn Smith 8515 gold badges19 silver badges37 bronze badges 8- Be aware, this should be closed because of duplicate: stackoverflow./questions/16813773/… You should remove your previous question. – A. Wolff Commented May 29, 2013 at 12:38
- Have you even read the ments on your exact same question @roasted has linked? – Andreas Commented May 29, 2013 at 12:40
-
What is the
result
you are getting back? You should be able to find it in Firebug's "Net" tab – andyb Commented May 29, 2013 at 12:42 - moderators marked it because they didn't understand what the question was, now I've corrected it and would be very grateful to obtain answers, but not minuses – John Smith Commented May 29, 2013 at 12:43
- 1 @JohnSmith you can delete your own question if you want - see meta.stackexchange./questions/5221/…. It is considered bad practice to repost the same question multiple times. It is better to re-write the question and ask for it to be re-opened - for example see meta.stackexchange./questions/115608/re-open-closed-question – andyb Commented May 29, 2013 at 12:50
3 Answers
Reset to default 4I don't think it is allowed to repost a question, anyway you should create your HTML Input not via string but via DOM, so you can attach the handler to the function and not on the "onclick".
$.ajax({
type: "POST",
url: "/getResult.json",
success: function(result) {
for (var i = 0; i < result.length; i++) {
(function (n) {
var obj = result[i],
element = $("<input>", {type: "checkbox"});
element.click(function () {
getPointMap(obj);
});
$(document.body).append(element, obj.address + "<br />");
})(i)
}
}
});
Ok, simple way:
html += "<input type='checkbox' onClick='getPointOnMap(" + obj.lattitude + ", " + obj.longtitude + ")'/>" + obj.address + "<br>";
function getPointOnMap(lat,lng) {
map.addMarker({
lat: lat,
lng: lng,
click: function(e) {
alert('You clicked in this marker');
}
});
}
$.ajax({
type: "POST",
url: "/getResult.json",
success: function(result) {
var myDiv = $('#myDiv');
$.each(result, function(i, obj) {
myDiv.append(
$('<INPUT>').attr('type', 'checkbox').on('click', function() {
getPointOnMap(obj);
},
obj.address,
"<br>"
);
});
}
});