It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Problem with a function

Problem with a function

Postby wavelink on Mon Sep 24, 2007 1:32 am

I'm trying to write a function that will when toggle between to form elements when an event is called. Here's a sample of what I'm doing.

<html>
<head>
<script type="text/javascript">
function tuggle()
{
if (document.getElementById("numbers").disabled="true")
{
document.getElementById("fruit").disabled="false";
}
else
{
document.getElementById("numbers").disabled="false";
}
}

</script>
</head>
<body>

<form>
<select id="fruits">
<option>Apple</option>
<option>Pear</option>
<option>Banana</option>
<option>Orange</option>
</select>

&nbsp; &nbsp;

<select id="numbers" disabled="true">
<option>1</option>
<option>2</option>
<option>3</option>
<option>5</option>
</select>

<br /><br />

<input type="button" onclick="tuggle()" value="Tuggle">
</form>

</body>
</html>

any help please?[code][/code]
wavelink
 
Posts: 8
Joined: Mon Sep 24, 2007 1:17 am

Re: Problem with a function

Postby Logician on Mon Sep 24, 2007 1:23 pm

wavelink wrote: if (document.getElementById("numbers").disabled="true")
You need to learn the difference between assignment and comparison operators. http://developer.mozilla.org/en/docs/Co ... _Operators

Also true and false are booleans not strings
Logician
 
Posts: 20
Joined: Tue Jun 12, 2007 3:43 am
Location: U.K.

Postby matt_raleigh on Wed Sep 26, 2007 11:57 am

here is what is meant by assignment and comparison operators

<html>
<head>
<script type="text/javascript">
function tuggle()
{
if (document.getElementById("numbers").disabled=="true")
{
document.getElementById("fruit").disabled=="false";
}
else
{
document.getElementById("numbers").disabled=="false";
}
}

</script>
</head>
<body>

<form>
<select id="fruits" >
<option>Apple</option>
<option>Pear</option>
<option>Banana</option>
<option>Orange</option>
</select>

&nbsp; &nbsp;

<select id="numbers" disabled="true">
<option>1</option>
<option>2</option>
<option>3</option>
<option>5</option>
</select>

<br /><br />

<input type="button" onclick="tuggle()" value="Tuggle">
</form>

</body>
</html>

Hopefully this will help you finish your project
User avatar
matt_raleigh
 
Posts: 15
Joined: Thu Sep 20, 2007 6:45 pm

Postby wavelink on Wed Sep 26, 2007 11:07 pm

Thanks guys, I really appreciate your help. I see what you mean! I’m suppose to set the comparison in the statement section (), and set my instructions with in the curly brasses. But I can’t get this pice of code to work! I got it to work to some degree, by simplifying the function using two buttons and one drop down menu. One button enabling and the other disabling. I even managed to write another function that would check to see if the drop down menu is enabled or disabled in conjunction with the previous one. However for what ever reason I just can't get this toggle function to work! Here's my latest attempt. As you can see, I'm only comparing the parameter in the "if" statement using the double equal sign ==. In the "if" instruction line and the "else" instruction line I'm setting the boolean values, so I'm using only one equal sign "="

function toggle()
{
If(document.getElementById("numbers").disabled==true)
{
document.getElementById("fruits").disabled==false
}
else
{
document.getElementById("numbers").disabled==false
}
}

However I''m still getting an error message.
wavelink
 
Posts: 8
Joined: Mon Sep 24, 2007 1:17 am

Postby wavelink on Wed Sep 26, 2007 11:10 pm

Sorry, I uploaded the wrong function, though it dose not make much difference, since neither work!

function toggle()
{
If(document.getElementById("numbers").disabled==true)
{
document.getElementById("fruits").disabled=false
}
else
{
document.getElementById("numbers").disabled=false
}
}
wavelink
 
Posts: 8
Joined: Mon Sep 24, 2007 1:17 am

Postby Logician on Thu Sep 27, 2007 12:09 am

wavelink wrote: If(document.getElementById("numbers").disabled==true)
{
It's also imperative to learn to use the JavaScript console, or you'll spend hours getting nowhere. I suspect it's saying something like " 'IF' is undefined or not a function".
Logician
 
Posts: 20
Joined: Tue Jun 12, 2007 3:43 am
Location: U.K.

Postby wavelink on Thu Sep 27, 2007 1:32 am

You are right! It said that I have a syntax error at my "else". What could it be?

function toggle()
{
If(document.getElementById("numbers").disabled==true)
{
document.getElementById("fruits").disabled=false;
}
else
{
document.getElementById("numbers").disabled=false;
}
}
wavelink
 
Posts: 8
Joined: Mon Sep 24, 2007 1:17 am

Postby Logician on Thu Sep 27, 2007 1:40 am

wavelink wrote:You are right! It said that I have a syntax error at my "else". What could it be?

function toggle()
{
If(document.getElementById("numbers").disabled==true)
{
Effectively else without if. JS is a case-sensitive language.
Logician
 
Posts: 20
Joined: Tue Jun 12, 2007 3:43 am
Location: U.K.

Postby wavelink on Thu Sep 27, 2007 1:51 am

Wow...unbelievable... alas, the error message is gone, but the toggle function still does not work. Thank you nonetheless, I still consider this progress though. I've gained the use of the js console and a better better understanding of the language, thought I realize I still have a long way to go.
wavelink
 
Posts: 8
Joined: Mon Sep 24, 2007 1:17 am

Postby matt_raleigh on Thu Sep 27, 2007 11:57 am

just as a fyi, whenever you get an error message and it tells you where it is and you don't see anything wrong with that statement always look above where the error is supposed to be, if that makes any sense to you.

for example, the console said that you had an error at your else statement, but in reality the problem was with your if statement being If instead of if

hope this helps you out
User avatar
matt_raleigh
 
Posts: 15
Joined: Thu Sep 20, 2007 6:45 pm

Next

Who is online

Users browsing this forum: No registered users and 6 guests