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 03-11-2012, 12:19 PM   PM User | #1
helloJS
New to the CF scene

 
Join Date: Mar 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
helloJS is an unknown quantity at this point
Smile How to transform 2 similar functions in only one ?

Hello, I have the code in the annexe wich is working but grossly redondant, clearly, I would like to have only one function toggleEditor(), that would manage the global variables editInstance with a parameter or something equivalent to avoid having 2 twin like functions. Thanks for your suggestions.

HJS


Annexe :
Code:
 
<html>
<body>

<script src="../flovinax.js" type="text/javascript"></script>
<script>
var editInstance= null;
function toggleEditor1(edtN) {
	if(editInstance===null) {
		editInstance = new flovinaxa({fullPanel : true}).panelInstance(edtN);
	} else {
		editInstance.removeInstance(edtN);
		editInstance= null;
	}
}

var editInstance2= null;
function toggleEditor2(edtN) {
	if(editInstance2===null) {
		editInstance2 = new flovinaxa({fullPanel : true}).panelInstance(edtN);
	} else {
		editInstance2.removeInstance(edtN);
		editInstance2= null;
	}
}
</script>	


<div>
	<textarea style="width: 800px; height: 200px;" id="myArea"></textarea>
	<br />
	<button onClick="toggleEditor1('myArea');">+</button>
</div>
<div style="clear: both;"></div>


<div>
	<textarea style="width: 800px; height: 200px;" id="myArea2"></textarea>
	<br />
	<button onClick="toggleEditor2('myArea2');">+</button>
</div>
<div style="clear: both;"></div>

</body>
</html>
helloJS is offline   Reply With Quote
Old 03-11-2012, 01:11 PM   PM User | #2
helloJS
New to the CF scene

 
Join Date: Mar 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
helloJS is an unknown quantity at this point
Lightbulb I got it !!!

I got it ... (create an object so it's passed with the address) how asking is already solving a bit! Here we are (code tested and ok) :

Code:
<html>
<head>
</head>
<body>


<script src="../flovinax.js" type="text/javascript"></script>

<script type="text/javascript">
function toggleEditor(edtX, edtN) {
	if(edtX.edt===null) {
		edtX.edt = new flovinaxa({fullPanel : true}).panelInstance(edtN);
	} else {
		edtX.edt.removeInstance(edtN);
		edtX.edt= null;
	}
}
</script>	


<h4>Textarea Example</h4>
<div>
	<textarea style="width: 800px; height: 200px;" id="boxA"></textarea>
	<br />
	
	<script type="text/javascript">
		edtA={edt:null};	// THE OBJECT edtA
	</script>	
	
	<button onClick="toggleEditor(edtA,'boxA');">+</button>
</div>
<div style="clear: both;"></div>



<h4>Textarea Example</h4>
<div>
	<textarea style="width: 800px; height: 200px;" id="boxB"></textarea>
	<br />
	
	<script type="text/javascript">
		edtB={edt:null};	// THE OBJECT edtB
	</script>		
	
	<button onClick="toggleEditor(edtB,'boxB');">+</button>
</div>
<div style="clear: both;"></div>



</div>

</body>
</html>
helloJS is offline   Reply With Quote
Old 03-11-2012, 02:22 PM   PM User | #3
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,358
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<html>
<body>

<script src="../flovinax.js" type="text/javascript"></script>
<script>

function toggleEditor(edtN) {
	if(!toggleEditor[edtN]) {
		editInstance = new flovinaxa({fullPanel : true}).panelInstance(edtN);
        toggleEditor[edtN]=true
	}
    else {
		editInstance.removeInstance(edtN);
		toggleEditor[edtN]= false;
	}
}

</script>


<div>
	<textarea style="width: 800px; height: 200px;" id="myArea"></textarea>
	<br />
	<button onClick="toggleEditor('myArea');">+</button>
</div>
<div style="clear: both;"></div>


<div>
	<textarea style="width: 800px; height: 200px;" id="myArea2"></textarea>
	<br />
	<button onClick="toggleEditor('myArea2');">+</button>
</div>
<div style="clear: both;"></div>

</body>
</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Reply

Bookmarks

Tags
functions, similar, transform

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 09:03 PM.


Advertisement
Log in to turn off these ads.