0

Please be patient throughout this, I am VERY bad at coding both JavaScript and HTML, and this problem is probably trivial to anyone who knows anything.

For a school project, I have to create a website with a JavaScript, interactive feature. I'm planning on making a button that subsequently, when pressed, prompts the user many questions, such as "Do you have an account?", "Username?", and "Password". I believe my JavaScript is written correctly, but when I link it in my HTML, and when I press the button that is intended to start this prompting, nothing happens. What am I doing wrong?

Here are segments of both my HTML and JavaScript code that I am using.

JavaScript

function LogIn(){
var Account = prompt("Do you have an account?")

if (Account = "yes"){
    var Username = prompt("Username");
    if (Username = "MarkColley"){
        var Password = prompt("Password");

...

else{
    confirm("That keyword is not acceptable. Try 'yes' or 'no' instead.");
}
document.getElementById("demo").innerHTML = text;
}

HTML

<li><a href="CurrentShows.html">Current Shows</a></li>
<li><img src="https://cdn4.iconfinder.com/data/icons/man-and-woman/154/man-human-person-login-512.png" style="width:20px;height:20px;"><button onclick="Login()">Sign Up/Log In</button></a></li>
<script type="text/javascript" src="LogInSignUp.js"></script>
</ul>

If it helps at all, here is an image of what the button/area that I am trying to get to work looks like.

An image of my unworking, useless button.

2
  • 1
    I recommend working through some basic JavaScript tutorials. The difference between = and ==/=== is covered really early on. Commented Dec 10, 2015 at 23:12
  • 1
    Side notes on markup: You have an </a> that has no <a>, and be careful using button: If you put it in a form, the default value of type for button is submit (e.g., if you put it in a form and don't say type="button", it will submit the form). Commented Dec 10, 2015 at 23:22

2 Answers 2

5

Here's a fiddle with a fixed version of your code, now it works.

http://jsfiddle.net/e3xpy5tq/

What didn't work:

  1. You used assignment instead of comparison (= instead of ==).

  2. You missed a semicolon and a couple curly brackets.

  3. Your function LogIn was misspelled on the HTML (Login instead of LogIn; JavaScript is case-sensitive).

    function LogIn() {
       var Account = prompt("Do you have an account?");
       if (Account == "yes") {
           var Username = prompt("Username");
           if (Username == "MarkColley") {
               var Password = prompt("Password");
           } else {
               confirm("That keyword is not acceptable. Try 'yes' or 'no' instead.");
           }
           document.getElementById("demo").innerHTML = text;
       }
    }
    

A couple suggestions:

As mentioned in some comments, go through JavaScript and HTML tutorials. Also, use a code linter in your favourite text editor. I use Sublime Text with one of its many plugins. It will help you find your errors by yourself.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow! Thanks! That solved every single one of my problems! Thank you so much - my life is saved!
You're welcome. Please take my advice on code linters into account too. You could also accept the answer, tehee.
Haha yeah I had another problem with my code (used if else, not else if), and I used a code linter to fix it. Thanks for the advice! As for the tutorials, I have done numerous HTML tutorials, a basic JavaScript one, but neither of them covered how to link the program.
0

To make the function fire you are to go this way:

<a href="#!" onclick="LogIn()">Click</a>

And to check if a value is equal to another one, you are to use the equal signs:

if(value_one === value_two)....

Because if you use the single equal sign, you are actually assigning that value to a variable.

So if you go:

if (Account = "yes"){...

You are assigning to Account the value of yes.

1 Comment

I tried doing this, but now when I click the link it just links to a raw word file of the JavaScript, but it doesn't run it. I may not be following your instructions correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.