0

I have a question that I'm pretty sure is basic, but can't seem to find the answer to it anywhere. I have an html page with two buttons. Ideally, I would have them show/do two different things (both are popup windows), but I find that I can't even get the second button to work when they share the exact same code.

Here's the code (I've omitted the css):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf=8"/>
<center>
    <nav>
        <a href="#loginmodal" class="flatbtn" id="modaltrigger">LOGIN</a>
        <a href="#contactmodal" class="flatbtn" id="modaltrigger">CONTACT</a>
    </nav>
</center>

<!--script for checklogin--> 
<script type = "text/javascript">

    function checkLogin(){
        //All this (index.php, validate.js, and login.php) taken from: /*http://stackoverflow.com/questions/15513031/authentication-using-ajax-php*/  
        var u = document.getElementById("username").value;
        var p = document.getElementById("password").value;
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                if(xmlhttp.responseText == u){
                    alert(':)');
                }
                else{
                    alert(':(');               
                }
            }
        }
        xmlhttp.open("GET", "checkpw.php?u=" + u + "&p=" + p, true);
        xmlhttp.send(); 
    }
    function newUser(){
        location.href = 'signup.php';
    }
</script>
</head>

<body>
<center>

<!--Login-->
<div id="loginmodal" style="display:none;">
  <logh1>Sign In</logh1>
  <form id="loginform" name="loginform" method="post" action="index.html">
    <label for="username"><h2>Username:</h2></label>
    <input type="text" name="username" id="username" class="txtfield" tabindex="1">
    <label for="password"><h2>Password:</h2></label>
    <input type="password" name="password" id="password" class="txtfield" tabindex="2">

  <div class="center">

  <input type="submit" name="loginbtn" id="loginbtn" class="flatbtn-blu hidemodal" value="Log In" onclick="checkLogin(); return false;" tabindex="3">
  <input type="submit" name="newusrbtn" id="create-user"" class="flatbtn-blu hidemodal" value="New User" onclick="newUser();" tabindex="4">
  </div>
  </form>
</div>

<!--Contact-->
<div id="contactmodal" style="display:none;">
  <logh1>Sign In</logh1>
  <form id="contactform" name="contactform" method="post" action="index.html">
    <label for="username"><h2>Username:</h2></label>
    <input type="text" name="username" id="username" class="txtfield" tabindex="1">
    <label for="password"><h2>Password:</h2></label>
    <input type="password" name="password" id="password" class="txtfield" tabindex="2">

  <div class="center">

  <input type="submit" name="loginbtn" id="loginbtn" class="flatbtn-blu hidemodal" value="Log In" onclick="checkLogin(); return false;" tabindex="3">
  <input type="submit" name="newusrbtn" id="create-user"" class="flatbtn-blu hidemodal" value="New User" onclick="newUser();" tabindex="4">
  </div>
  </form>
</div>
</center>

<script type="text/javascript">
    $(function(){
      $('#loginform').submit(function(e){
        return false;
      });

    $('#contactform').submit(function(e){
        return false;
      });

      $('#modaltrigger').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
    });
</script>
</body>
</html>

All I did was change the following for the second (contact) button: div id, form id, form name (all from <!--Login--> to <!--Contact-->). What I noticed when I loaded my page was that the first (login) button worked just fine, but when I closed its dialog box and pressed the second (contact) button, nothing would happen, but the following would show up on my url:

...login.php#contactmodal

(vs the original url of ...login.php)

Could someone please tell me what's going on, and why my second button isn't working? Also, because I'm a beginner, any opinions on style or syntax are appreciated as well.

2
  • FYI, you can use inline code spans (surround with backticks) to get around the problem of nonprinting characters. You can also use &lt; to get a < (but you don't need &gt; for >; that one shows up unless it closes an HTML tag). Commented Sep 14, 2013 at 1:16
  • Actually, I wasn't trying to do that at all, I was only trying to save myself the trouble of re-writing my variables. What I was trying to say was that I changed loginmodal and loginform to contactmodal and contactform, that's all. Commented Sep 14, 2013 at 1:33

1 Answer 1

2

You have 2 elements with the same ID modaltrigger, ID of elements must be unique in a page.

Use class attribute instead to group similar elements.

    <a href="#loginmodal" class="flatbtn modaltrigger">LOGIN</a>
    <a href="#contactmodal" class="flatbtn modaltrigger">CONTACT</a>

then

$('.modaltrigger').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
Sign up to request clarification or add additional context in comments.

3 Comments

That worked like a charm, thank you! So is it always the case that id attributes have to be unique, but that class ones don't? I also understand why you changed from #modaltrigger to .modaltrigger, and will keep that in mind for the future...
@user2680503 yes... id is a unique identifier... so it has to be unique
Hey, do you know if this is also true ACROSS DIFFERENT pages within the same folder? I find myself coming across a similar issue cutting and pasting this code on another page...

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.