Page 1 of 1

javascript simple problem??

Posted: Fri Dec 15, 2006 1:11 pm
by sarris
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a weird problem. I have this javascript function. Area0 Area1 etc are drop down boxes.

[syntax="html"]<script language = javascript >
var areanum = -1
function addArea(){

	areanum = areanum + 1
	if(areanum == 0) {document.form2.Area0.disabled= false}
	if(areanum == 1) {document.form2.Area1.disabled= false}
	if(areanum == 2) {document.form2.Area2.disabled= false}
	if(areanum == 3) {document.form2.Area3.disabled= false}
	if(areanum == 4) {document.form2.Area4.disabled= false}
	if(areanum == 5) {document.form2.Area5.disabled= false}
	
}

</script>
I have a button <a href = "javascript:addArea()"><input type="button" value="Add Area" name="B3"></a> calling the function
What this is supposed to do is, that every time i press the button one more drop down list is enabled.
For some reason every time i press the button 2 drop down boxes are enabled....WHY??
Moreover does anyone have an idea how to make the disables boxes invisible and visible instead of enabled-disabled?


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Dec 15, 2006 5:18 pm
by feyd
Is there a reason you are using a link surrounding a button instead of the button itself?

You can possibly manipulate the display or visibility properties in the style object for each select element.

Posted: Fri Dec 15, 2006 5:29 pm
by sarris
No, not really a reason. how else can i call the function at the button click?

Posted: Fri Dec 15, 2006 5:43 pm
by feyd
That's what I was referring to...

Posted: Fri Dec 15, 2006 6:30 pm
by sarris
did it with
onclick = "addarea();"
and works fine...
sorry for the silly questions but i am just learning.
thanks

Posted: Fri Dec 15, 2006 6:34 pm
by feyd
Is the function being called twice still?

Other notes on your code:
  • each statement should end in a semicolon.
  • document.forms['formName'].elements['elementName'].disabled is the more compatible and standard way of accessing the settings.

Posted: Fri Dec 15, 2006 6:36 pm
by sarris
no its called only once...maybe cause of the semicolon??
yes this is the way i access the elements.
thanks.all works ok now

Posted: Fri Dec 15, 2006 6:38 pm
by feyd
No, that's not how you are accessing the elements. You are accessing them in this fashion: document.formName.elementName.disabled.

Posted: Fri Dec 15, 2006 6:39 pm
by sarris
oooh ok...now i see.
is there any big difference?

Posted: Fri Dec 15, 2006 6:41 pm
by feyd
One is standards compliant, the other isn't if memory serves. It works in several browsers because of the lax nature in which they have implemented Javascript.

Posted: Fri Dec 15, 2006 6:43 pm
by sarris
ok!!thanks for letting me know...can you check this topic as well plz in case you know the answer.if you cant bother its ok.
Thanks anyway for the really helpfull tips

viewtopic.php?p=339860#339860