...

View Full Version : Javascript function help: trying to get a variable to change if it's set



thepip3r
03-23-2005, 04:09 PM
Overall Purpose: I want a clickable button that inputs preformatted text into a submission field and the button should know whether it's open or closed...

Working Now: On the click of a hyperlink, I can get the text to display in the submission field like I want.

Problem: If the button has already been clicked and it's clicked again, the same information shows instead of adding a close tag to it.

This works:

in the head

<script>function addColor(color, chat, text) {
var revisedMessage;
var currentMessage = document.chat.elements[text].value;
revisedMessage = currentMessage+color;
document.chat.elements[text].value=revisedMessage;
document.chat.elements[text].focus();
return;
}
</script>
in the body

<a href="javascript:void[0];" onClick="addColor(':blue:','chat','text');">blue</a>

Right now the above script when the hyper link named, "blue" is clicked, it writes ":blue:" to the screen. What I still need to do is close ":blue:" somehow and I want it to happen if "blue" is clicked. If "blue" gets clicked a second time, it should write "/:blue:" to the screen or something like that.

I've tried this:

<script>function addColor(color, chat, text) {
if (color == openTag) {
closeTag = "/";
color = closeTag+color;
}
var revisedMessage;
var currentMessage = document.chat.elements[text].value;
revisedMessage = currentMessage+color;
document.chat.elements[text].value=revisedMessage;
document.chat.elements[text].focus();
var openTag = color;
return;
}
</script>

but no love... can anyone help?

[Z]
03-23-2005, 10:08 PM
function addColor(color,chat,text){
x=document.chat.elements[text];
x.value=(x.value==color)? '/'+x.value : ņolor;
x.focus();
}

thepip3r
03-23-2005, 11:20 PM
i don't quite understand how i'm supposed to incorporate that... would you mind elaborating a little?

[Z]
03-24-2005, 06:21 AM
i don't quite understand how i'm supposed to incorporate that... would you mind elaborating a little?
Replace your function...

thepip3r
03-24-2005, 03:01 PM
so the code you wrote above replaces my whole function? and i replace "x" in your function with everything preceding the "=" sign in my function right?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum