Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to do login and logout task. If User login then alert login value else alert Empty but it is not properly set. it always return "Empty" Value even though "gg" has value.


<button id="btn" onclick="Test()" style="width:150px; height:40px;background-color:blue;color:white;">Pay Online</button>


function Test(){

            var re= parseInt(sessionStorage.getItem("myUser1"));
            document.getElementById('loveuser').value=re;
            var gg=document.getElementById('loveuser');

            if(!isNaN(gg)){
                alert(gg);
            }else{
                alert("Empty");
            }

        }


"gg" always retrun "Empty" Not "gg" value even though it has value. What's wrong?.

What I have tried:

trying from two days via online
Posted
Updated 28-Feb-23 22:14pm
Comments
Member 15627495 1-Mar-23 1:31am    
it seems "sessionStorage.getItem("myUser1")" is empty..
your isNaN works.

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/isNaN
MAHESH WAGHELA 1-Mar-23 2:46am    
Thx Its Now Working

1 solution

Quote:
JavaScript
var gg=document.getElementById('loveuser');
if (!isNaN(gg)) {
Your gg variable contains the HTML element itself, not the value of that element.

An HTML element is not a number, so your code will always display "Empty".

Change your code to test the value of the element instead.
JavaScript
var gg = document.getElementById('loveuser').value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900