Matthew Walker Matthew Walker - 6 months ago 14
Javascript Question

JS variable returning object not value

I'm setting a global variable via jQuery as such:

user = $("#user").val();


Specifically without
var
so that it can be pulled outside of
function
.

When I try to pull that variable later all I get is
[object HTMLInputElement]
.

Here is the code I'm using to pull that variable,

<script type="text/javascript">
document.getElementById("user-login-info").innerHTML = "Hello, "+user;
</script>


What am I doing wrong here?

Answer

I have checked it and modified a little

$("#login-submit").click(function() {
                    user = $("#user").val();
                    pass = $("#pass").val();
                    var dataString = "user="+user+"&pass="+pass;
                    var linkString = "index.html#home";
                    if (user == "" || pass == "") {
                        alert("Please fill in all fields");
                    }
                    else {
                        $.ajax({
                            type: "POST",
                            url: "http://www.weddingindustryinsider.com/VAWEAPP/login.php",
                            data: {user: user, pass: pass},
                            cache: false,
                            success: function(result) {
                                if (result = "1") {
                                    alert("Login Sucess.");
                                    setTimeout(location.href = linkString,1000);
                                }
                                else { alert("Username or Password incorrect. Please try again."); }
                            },
                        });
                    }
                    return false;
                });
Comments