Jump to content

[SOLVED] If statements issue


Eggzorcist

Recommended Posts

I'm sorry it's very basic it's just its for a school project and I can't seem to get it to work.

 

here my code

 

function showUser(str)
{


if (str==5){

string = "Name: Harold<br>Location: Montreal, Canada"

document.getElementById('txtHint').innerHTML = string;

}



else {

alert("Wrong ID, Please try again");
}

}

 

I can't seem to find the issue.

 

Thanks

Link to comment
Share on other sites

I'm sorry it's very basic it's just its for a school project and I can't seem to get it to work.

 

here my code

 

function showUser(str)
{


if (str==5){

string = "Name: Harold<br>Location: Montreal, Canada"

document.getElementById('txtHint').innerHTML = string;

}



else {

alert("Wrong ID, Please try again");
}

}

 

I can't seem to find the issue.

 

Thanks

 

Let me guess... your 'str' value comes from some form field or query string, right?  If so, it's being treated as a string and not a number.  To fix it, run it through the parseInt() function:

 

function showUser(str)
{
   str = parseInt(str);

   if(str == 5)
   {
      string = "Name: Harold<br>Location: Montreal, Canada";
      document.getElementById('txtHint').innerHTML = string;
   }
   else
   {
      alert("Wrong ID, Please try again");
   }
}

Link to comment
Share on other sites

I've added that however, the command still gets passed to the else command.

 

I know this isn't the problem but here is how the information is showing.

 

 

 <form>
Package ID:<input name="id" id='id' type="text" size="10" maxlength="10" /> 
<br />
<input type='button' onclick='showUser(this.value)' value="Track"/>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.