Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Quick question about adding properties to strings and toggling!

    Hello, my JS knowledge is pretty limited. This should be pretty simple...
    I am trying to figure out if it is possible to add properties (or tie another variable) to a variable string? I believe it is limited to objects. I'll show you the code to help clarify what I am getting at:

    Code:
     
        function toggleKml(theKML) {	
          if (theKML.toggleState == 1) {
            map.removeOverlay(theKML);
            theKML.toggleState = 0;
          } else {
            map.addOverlay(theKML);
            theKML.toggleState = 1;
          }
        }
    This function works because theKML is an object. Here is my attempt at the same thing, but passing a string ("tabTitle") instead:

    Code:
    	function toggle_the_tabs(tabTitle) {
    		if (tabTitle.toggleState == 1) {
    			tabViewObj.deleteTab(tabTitle);
    			tabTitle.toggleState = 0;
    		} else {
    			tabViewObj.createNewTab('tab_container',tabTitle,'','some.txt');
    			tabTitle.toggleState = 1;
    		}
    	}
    And the above function is called by:

    Code:
    <input type="checkbox" name="tab_toggle"  onClick="toggle_the_tabs('A new tab');"/>
    <label for="tab_toggle"> Toggle tab</label>
    So, my work-around has been to create a dummy object for each item that I toggle. This is not very pretty. Any other suggestions?

    Thanks in advance!

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    a string is an object, instance of String and therefor you can use prototype to add something you want( see "Using prototype on prebuilt JavaScript objects"):

    http://www.javascriptkit.com/javatutors/oopjs2.shtml

    best regards

  • #3
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Oesxyl for the prompt reply! However, given the simplicity of what I was looking to do, I'd rather not use an additional framework such as prototype.

    In the end, the solution I came up with is pretty straightforward. I created a global variable object and then checked if it was set or not and that determined the toggle state. Works for multiple calls.

    Code:
    var togStatus = new Object;
    
    	function pleaseToggle(theTab) {
    		if (!togStatus[theTab]) {
                    togStatus[theTab] = 1;
    		tabViewObj.createNewTab('tabContainer',theTab,'','some.txt');
    		}else{
    		togStatus[theTab] = 0;
    		tabViewObj.deleteTab(theTab);
    		}
    	}
    If you see any potential problems with this, please let me know!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by _Z_U_ View Post
    Thanks Oesxyl for the prompt reply! However, given the simplicity of what I was looking to do, I'd rather not use an additional framework such as prototype.
    prototype is native javascript, and have nothing to do with prototype framework,

    In the end, the solution I came up with is pretty straightforward. I created a global variable object and then checked if it was set or not and that determined the toggle state. Works for multiple calls.

    Code:
    var togStatus = new Object;
    
    	function pleaseToggle(theTab) {
    		if (!togStatus[theTab]) {
                    togStatus[theTab] = 1;
    		tabViewObj.createNewTab('tabContainer',theTab,'','some.txt');
    		}else{
    		togStatus[theTab] = 0;
    		tabViewObj.deleteTab(theTab);
    		}
    	}
    If you see any potential problems with this, please let me know!
    I don't think there is a problem,

    best regards


  •  

    Tags for this Thread

    Posting Permissions

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