tqla Posted October 15, 2010 Share Posted October 15, 2010 I'm having difficulty with this function. I'm using jquery. The number in #cfp will be anything from 1 to 11,000. My function below should display the word Excellent, Good, Average or Poor depending on the number. But it doesn't work. It doesn't seem to be comparing the vars correctly and will show two words at times. What am I doing wrong? Thanks. To test it I've made the var rating var rating = '3000'; but it still doesn't quite work. Am I doing it right or is there a better way to do this comparison? function rating(){ var rating = $("#cfp").text(); if(rating < '2500'){ var myDiv1 = document.getElementById("rating"); myDiv1.appendChild(document.createTextNode("Excellent")); }else if(rating > '2501' && rating < '6999'){ var myDiv1 = document.getElementById("rating"); myDiv1.appendChild(document.createTextNode("Good")); }else if(rating > '7000' && rating < '10000'){ var myDiv1 = document.getElementById("rating"); myDiv1.appendChild(document.createTextNode("Average")); }else if(rating > '10001'){ var myDiv1 = document.getElementById("rating"); myDiv1.appendChild(document.createTextNode("Poor")); } }; Quote Link to comment https://forums.phpfreaks.com/topic/215975-help-with-comparing-the-value-of-a-div-id/ Share on other sites More sharing options...
tqla Posted October 15, 2010 Author Share Posted October 15, 2010 Never mind. This has to do with comparing 5 digit numbers to 4 digit numbers. Close to a solution. Quote Link to comment https://forums.phpfreaks.com/topic/215975-help-with-comparing-the-value-of-a-div-id/#findComment-1122611 Share on other sites More sharing options...
.josh Posted October 17, 2010 Share Posted October 17, 2010 var rating = parseInt($("#cfp").text()); Also, your order of operations in your conditions may be screwy... Do this (and to your other ones): else if((rating > '2501') && (rating < '6999')){ Quote Link to comment https://forums.phpfreaks.com/topic/215975-help-with-comparing-the-value-of-a-div-id/#findComment-1123046 Share on other sites More sharing options...
tqla Posted October 18, 2010 Author Share Posted October 18, 2010 Thanks Crayon Violent, the parseInt() was just what I was missing! Yes, my syntax was crummy too. Fixed that and it's working fine. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/215975-help-with-comparing-the-value-of-a-div-id/#findComment-1123212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.