Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-28-2009, 01:31 AM   PM User | #1
_Z_U_
New to the CF scene

 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
_Z_U_ is an unknown quantity at this point
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!
_Z_U_ is offline   Reply With Quote
Old 04-28-2009, 02:37 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Old 04-28-2009, 09:47 PM   PM User | #3
_Z_U_
New to the CF scene

 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
_Z_U_ is an unknown quantity at this point
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!
_Z_U_ is offline   Reply With Quote
Old 04-28-2009, 09:54 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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,

Quote:
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
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, object, string, toggle, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:29 AM.


Advertisement
Log in to turn off these ads.