Stack One
Stack One

Reputation: 25

reading value attached to a variable in Javascript

Hi below is a section of my code. I dont get the desired out put for else if ( txt3Val.value >= txt4Val ) but if I adjust it to else if ( document.getElementById('txt3') >= document.getElementById('txt4') ) it works. Can someone tell me why this happens? Thanks.

......AND THE CODE READS
    else if(document.form1.GWchk.checked == true) 
        {
        var txt3Val = document.getElementById('txt3');
        var txt4Val = document.getElementById('txt4');
            if(txt3Val.value == "" || txt4Val.value == "")
            {
                alert ("You need to enter both starting and ending number \nof the cheque book for GlW."); return false;
            }
            else if ( txt3Val.value >= txt4Val ) 
            { 
                alert ("Ending cheque number should be of greater value"); return false;
            }
            else
            {
            alert ("Successfully saved your entry1."); return false;
            }
        }

HIGHLIGHT : Sorry the code else if ( txt3Val.value >= txt4Val.value ) also doesnt work! Actually that's what its there on my real script a re-write error here. But the my preoblem remains the same where it doesnt give me the ending number should be greate.... and straight goes to successfully saved.

EDIT I think I figure out something. On the first text box if I put '10' as a value and second as '9' then JS seems doesn't recognize 9 as a lower value. But if I put '09' it gives the desired out put. How do I handle this?

Upvotes: 0

Views: 73

Answers (4)

aziz punjani
aziz punjani

Reputation: 25776

if( parseInt( txt3Val.value, 10 ) >=  parseInt( txt4Val.value, 10 ) )

Upvotes: 0

AnandVeeramani
AnandVeeramani

Reputation: 1556

try changing:

else if ( txt3Val.value >= txt4Val ) 

to

else if ( txt3Val.value >= txt4Val.value ) 

Upvotes: 3

David G
David G

Reputation: 96810

Using comparisons again HTML elements isn't going to give you your desired result. Append .value to the end of txt4Val to access the value from that element:

else if ( txt3Val.value >= txt4Val.value ) {

}

Upvotes: 0

st3inn
st3inn

Reputation: 1596

else if ( txt3Val.value >= txt4Val )

should be

else if ( txt3Val.value >= txt4Val.value )

Upvotes: 2

Related Questions