confused and dazed Posted November 17, 2011 Share Posted November 17, 2011 Hello folks.Here is my problem.I have a bunch of hidden id's with values but I need to check to see if those values are right before I submit my form. I tried an if statement but it is just not working. I am trying something like Function checkquad(){if(r1.value="");if(r2.value="");{alert("r1 and r2 not filled out right");}} What I am trying to say with my if statements is that if the value r1 and r2 (from a from I named tourney) do not have a value set yet then send out the alert and dont submit the form... Not working... Link to comment Share on other sites More sharing options...
justsomeguy Posted November 17, 2011 Share Posted November 17, 2011 r1 and r2 are not defined in that function. Give the inputs IDs and access them using document.getElementById. http://www.w3schools.com/jsref/met_doc_getelementbyid.asp Link to comment Share on other sites More sharing options...
confused and dazed Posted November 17, 2011 Author Share Posted November 17, 2011 I've done this already in the function - so I was trying to get thier values... <input type="hidden" name="r1" id="r1" value"" /><input type="hidden" name="r2" id="r2" value"" /> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 17, 2011 Share Posted November 17, 2011 Putting tags on the page doesn't define arbitrary variables in every Javascript function. You need to actually get references inside Javascript to the HTML elements on the page. HTML and Javascript aren't the same thing. Check the link I posted. Your if statement also isn't structured correctly, check the tutorial about if statements: http://www.w3schools.com/js/js_if_else.asp Note that you don't put a semicolon after the condition. Writing this:if(r1.value=""); Is the same as doing nothing: if(r1.value=""){ } Example 3 on the above link shows an example where they test two conditions with an AND, but you probably want to use OR. This page has examples of using AND and OR operators: http://www.w3schools.com/js/js_comparisons.asp Also on that page, look at the == operator. That's the one you need to use in an if statement. When you use = it assigns a value, when you use == it compares values. Link to comment Share on other sites More sharing options...
confused and dazed Posted November 17, 2011 Author Share Posted November 17, 2011 O.K. i was able to fix my comparison issue with ==, thanks. But I still cannot seem to pull the value of r1 into the value for quad function show_alert(){quad ="document.tourney.r1.value"; //tourney is the name of the form where r1 exsists if(quad=="higher") //higher is the value of the hidden value r1 { alert("Hello! yes"); } else { alert("nope"); }} Link to comment Share on other sites More sharing options...
justsomeguy Posted November 17, 2011 Share Posted November 17, 2011 For the third time, look at the tutorial and use document.getElementById. var r1 = document.getElementById('r1');alert(r1.value); Link to comment Share on other sites More sharing options...
confused and dazed Posted November 17, 2011 Author Share Posted November 17, 2011 I looked at the tutorial before and tried it but it was not working - I guess I will keep working on it. Thank you. Link to comment Share on other sites More sharing options...
confused and dazed Posted November 17, 2011 Author Share Posted November 17, 2011 I got it Needed a .value added to my variable all good now!!!! Link to comment Share on other sites More sharing options...
Ingolme Posted November 17, 2011 Share Posted November 17, 2011 First, you have to get the value of it:if(quad.value != "") {Also, you might be trying to check the value before you have set it. Link to comment Share on other sites More sharing options...
confused and dazed Posted November 17, 2011 Author Share Posted November 17, 2011 That is exactly what was going on... THANKS. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.