0

first.html

<!DOCTYPE html>
<html>

</head>

<body>
            <form>
                <script type="text/javascript">

                    // grab the id number
                    var theIdNumber = localStorage.getItem("idNumber");

                    // set the ID in the HTML page
                    document.getElementById("userId").value = theIdNumber;
                </script>

                <button type="button" onClick="validateUsernamePassword()">SEND</button>
            </form> 
</body>
</html>

other.js

function validateUsernamePassword()
{    
            var idNum = $("#userId").val(); 
            alert('Well the id is :' + idNum);

            // more code 
}

The alert doesn't print the passed ID ... why ?

4
  • Because there is no element with the ID "userId" anywhere in your html document. Commented Feb 9, 2014 at 8:32
  • @ron where is element with id of userId ? Commented Feb 9, 2014 at 8:33
  • @ Amit Joki: Aha , then I can't just declare an element userId in the JS code... got it . How can I fix it ? Commented Feb 9, 2014 at 8:35
  • You already have the userid stored in local storage. Why not just pull it back out in your validateUsernamePassword method? Commented Feb 9, 2014 at 8:37

2 Answers 2

2

Modify your html like this:

<html>
<head>
<title></title>
</head>
        <body>
                    <form>


                        <button type="button" onClick="validateUsernamePassword()">SEND</button>
                        <input type="hidden" id="userId"></input>
                    </form> 
                        <script type="text/javascript">

                            // grab the id number
                            var theIdNumber = localStorage.getItem("idNumber");

                            // set the ID in the HTML page
                            document.getElementById("userId").value = theIdNumber;
                        </script>
        </body>

    </html>

Or you could do just this:

function validateUsernamePassword()
{    
            var idNum = localStorage.getItem("idNumber"); 
            alert('Well the id is :' + idNum);

            // more code 
}

The second one is good since you already have the item in localstorage, so ther is no need of assign its value to another element

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

Comments

0

this trick may help you:

<html>
<head>

</head>
<body>
<dl>
<script>
    window.var1 = "hello, world";

</script>
<input type="button" value="click" onclick="clicked()">

<script>
    function clicked(){
        alert(window.var1);
    };

</script>
</body>

Comments

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.