PDA

View Full Version : how to make this function checked by default?



dlbhome
10-22-2013, 02:18 PM
$(document).ready(function (){
$(':checkbox').click(function(){
var id = $(this).attr('id');
if($(this).is(':checked')){

var vv = $(this).val();
vv = vv.split(",");
vv = new GLatLng(vv[0], vv[1]);
// var vl2 = "(" + v1 + ")"
//alert(vl2);
var markerlatlng= vv;
console.log(markerlatlng);
var mark = new GMarker(markerlatlng, true);
markers.push({"id": id, "marker": mark});
map.addOverlay(mark);
map.setCenter(vv);
} else {
for(var i=0; i< markers.length; i++){
if(markers[i].id == id) {
map.removeOverlay(markers[i].marker);

jscheuer1
10-22-2013, 02:36 PM
What that does is listen to each checkbox on a page and does something when it's clicked. One thing is done if it's checked (presumably adds a marker to the map), another if it's not (presumably removes a marker from the map).

Do you mean that you want each checkbox on the page to be looked at when the page loads to see what state it is in and carry out its click function accordingly? Or do you mean that you want all checkboxes on the page to be checked and all markers to be on the map to begin with? Or perhaps only a certain checkbox or boxes? How many checkboxes do you have?

In any case, what you have there in your post isn't even a complete function call for the click event, let alone the document ready event. And it can get confusing because, depending upon which version of jQuery you're using, you may have to check or uncheck the box first before doing anything else. It might be easiest to set things up how you want them first, and then add the listener for the click. But, as I say, to do that we need to know how you want things at first. So please answer the above questions.

And if you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

dlbhome
10-22-2013, 03:11 PM
I would like all check boxes to be checked by default so the markers are on the map when the page loads. Then If the user removed one or more checks the markers are removed.

jscheuer1
10-22-2013, 03:21 PM
Add the highlighted:


$(document).ready(function (){
$(':checkbox').each(function(i, cb){
cb.checked = true;
var vv = cb.value.split(",");
vv = new GLatLng(vv[0], vv[1]);
var mark = new GMarker(vv, true);
markers.push({"id": cb.id, "marker": mark});
map.addOverlay(mark);
}).click(function(){
var id = $(this).attr('id');
if($(this).is(':checked')){

var vv = $(this).val();
vv = vv.split(",");
vv = new GLatLng(vv[0], vv[1]);
// var vl2 = "(" + v1 + ")"
//alert(vl2);
var markerlatlng= vv;
console.log(markerlatlng);
var mark = new GMarker(markerlatlng, true);
markers.push({"id": id, "marker": mark});
map.addOverlay(mark);
map.setCenter(vv);
} else {
for(var i=0; i< markers.length; i++){
if(markers[i].id == id) {
map.removeOverlay(markers[i].marker);

Untested but should work.

dlbhome
10-22-2013, 03:30 PM
This only checks the first checkbox and does not put the markers on the map.

jscheuer1
10-22-2013, 03:33 PM
I had an error I saw in my added code. I've just corrected it. I left out:


vv = new GLatLng(vv[0], vv[1]);

Refresh this page, go back to my last post, use the corrected code found there and see if it works now.

The browser cache may need to be cleared and/or the page refreshed to see changes.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

dlbhome
10-22-2013, 04:00 PM
http://www.solddata2.com/NCitySetup/mapsearch/default.asp

Draw a polygon around G street and click search

dlbhome
10-22-2013, 04:20 PM
Would it be easier to do a page onload function to check all the boxes?

jscheuer1
10-22-2013, 05:15 PM
Well, the code works now (if I paste it into the console), however it's executing too soon where I said to put it. The map isn't ready. We could try on load instead of document ready, but the map still might not be ready. Is there code that initializes the map and does it return a ready function? Looks like there isn't, but there is this, try adding it (highlighted) here:


function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(36.028711, -95.982683), 16);
map.addControl(new TextualZoomControl());
featureTable_ = document.getElementById("featuretbody");
select("hand_b");
$(':checkbox').each(function(i, cb){
cb.checked = true;
var vv = cb.value.split(",");
vv = new GLatLng(vv[0], vv[1]);
var mark = new GMarker(vv, true);
markers.push({"id": cb.id, "marker": mark});
map.addOverlay(mark);
});
}
}

dlbhome
10-22-2013, 05:41 PM
It makes no difference, I think it is still executing to soon. The page is using
Response.Write("<tr> <td class='data' width='10%'><input type=checkbox id='ldm' value='"&lat& ", " &lng&"' >
to write the checkboxes. I added checked to it
<td class='data' width='10%'><input type=checkbox id='ldm' value='"&lat& ", " &lng&"' checked>
and all boxes are checked, but it doesn't fire the function to add the overlay markers.

jscheuer1
10-23-2013, 12:43 AM
Best I can think of is to test for the availability of each and every thing that we need for it to work and to keep looping back to try again if they're not ready:


function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(36.028711, -95.982683), 16);
map.addControl(new TextualZoomControl());
featureTable_ = document.getElementById("featuretbody");
select("hand_b");
function setupcbmarkers(){
if(typeof map !== 'object' || typeof GLatLng !== 'function' || typeof GMarker !== 'function' || typeof map.addOverlay !== 'function'){
setTimeout(function(){setupcbmarkers();}, 300);
return;
}
$(':checkbox').each(function(i, cb){
cb.checked = true;
var vv = cb.value.split(",");
vv = new GLatLng(vv[0], vv[1]);
var mark = new GMarker(vv, true);
markers.push({"id": cb.id, "marker": mark});
map.addOverlay(mark);
});
}
setupcbmarkers();
}
}

The browser cache may need to be cleared and/or the page refreshed to see changes.

If it doesn't work, please leave the code in place. That might help me to troubleshoot it further.