Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: how to make this function checked by default?

  1. #1
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to make this function checked by default?

    Code:
    $(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);
    Last edited by jscheuer1; 10-22-2013 at 02:21 PM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Add the highlighted:

    Code:
    $(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.
    Last edited by jscheuer1; 10-22-2013 at 03:30 PM. Reason: correct code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    This only checks the first checkbox and does not put the markers on the map.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I had an error I saw in my added code. I've just corrected it. I left out:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    http://www.solddata2.com/NCitySetup/...ch/default.asp

    Draw a polygon around G street and click search

  8. #8
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Would it be easier to do a page onload function to check all the boxes?

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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);
        });
      }
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

Similar Threads

  1. default radio checked
    By game in forum JavaScript
    Replies: 2
    Last Post: 09-07-2013, 06:14 AM
  2. Resolved Make div load as collapsed by default (animated collapse script)
    By FaithPuppeteer in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 08-10-2010, 02:59 AM
  3. Default function values
    By JasonDFR in forum PHP
    Replies: 2
    Last Post: 02-07-2009, 04:28 PM
  4. How to make CMotion Image Gallery II move by default?
    By ijis in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 04-20-2008, 12:32 PM
  5. Replies: 2
    Last Post: 08-31-2006, 12:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •