Connecting Tech Pros Worldwide Forums | Help | Site Map

Check that an element exists before doing somthing

Newbie
 
Join Date: Feb 2007
Location: Canada
Posts: 23
#1: Mar 1 '07
Back again. Looking to check to see if an element has been output by a previous function before I do something in another function.

Example:

if (document.form1.Rank2==exist) {
for (var i=0;i<airforce.length;i++) {
document.form1.Rank.options[i] = new Option(airforce[i].name, airforce[i].value);
document.form1.Rank2.options[i] = new Option(airforce[i].name, airforce[i].value);
}
else {
for (var i=0;i<airforce.length;i++) {
document.form1.Rank.options[i] = new Option(airforce[i].name, airforce[i].value);
}
}
}

It's probably some simple boolean thing, but I can't find the answer
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#2: Mar 1 '07

re: Check that an element exists before doing somthing


i am pretty sure you can just do
if (document.form1.Rank2) {
}
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#3: Mar 1 '07

re: Check that an element exists before doing somthing


example

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function dosomething() {
  3. if (checkobject("test")) {
  4.   alert("This object exists");
  5.   document.getElementById("test").value = "It Existed";
  6. }
  7. }
  8. function checkobject(obj) {
  9. if (document.getElementById(obj)) { return true; } else { return false; }
  10. }
  11. </script>
  12. <input type="button" value="test" onclick="dosomething()">
  13. <input type="text" id="test">
  14.  
Here I just made a function to return true or false after it checks the object by id.
Newbie
 
Join Date: Feb 2007
Location: Canada
Posts: 23
#4: Mar 1 '07

re: Check that an element exists before doing somthing


Ok. It no worky for me.
Expand|Select|Wrap|Line Numbers
  1. function checkobject(obj) {
  2. if (document.getElementById(obj)) { return true; } else { return false; }
  3. }
  4.  
  5. if (checkobject("Rank2")) {
  6. for (var i=0;i<airforce.length;i++) {
  7. document.form1.Rank.options[i] = new Option(airforce[i].name, airforce[i].value);
  8. document.getElementById("Rank2").options[i] = new Option(airforce[i].name, airforce[i].value);
  9. }
  10. }
  11. else {
  12. for (var i=0;i<airforce.length;i++) {
  13. document.form1.Rank.options[i] = new Option(airforce[i].name, airforce[i].value);
  14. }
  15. }
  16. }
Says an object expected
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#5: Mar 1 '07

re: Check that an element exists before doing somthing


did you put that function inside another function?
Newbie
 
Join Date: Feb 2007
Location: Canada
Posts: 23
#6: Mar 1 '07

re: Check that an element exists before doing somthing


Quote:

Originally Posted by iam_clint

did you put that function inside another function?

The "if" statment = yes.
The checkObj function itself = no.
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#7: Mar 1 '07

re: Check that an element exists before doing somthing


give me link to the page again


And paste the line of code thats having problems

and you have 1 too many } brackets unless thats closing the function there
Newbie
 
Join Date: Feb 2007
Location: Canada
Posts: 23
#8: Mar 1 '07

re: Check that an element exists before doing somthing


Check out this first:
How the listboxes should work

This is the one with new code:
if (checkobject("Rank2")) added

If U enter a guest number you'll see the box I want to manipulate according to the checkbox. If it's check I want just a textbox, if it's unchecked go thru the Options Array and populate the second listbox that only gets output if the checkbox is unchecked. But, if it is not output first, because the guest number is 1 or 0, the populate function errors out on the first call.
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#9: Mar 1 '07

re: Check that an element exists before doing somthing


Expand|Select|Wrap|Line Numbers
  1.  
  2. function guest(){
  3.         if (checkobject("gstchk")) {
  4.           var chk=document.getElementById("gstchk");
  5.           if (chk.checked==true){
  6.             alert(checkobject("Rank2box"));
  7.            document.getElementById("Rank2box").value = document.getElementById("Rank").value;
  8.     document.getElementById("fstname1").value=document.getElementById("fstname").value;
  9.     document.getElementById("lstname1").value=document.getElementById("lstname").value;
  10.    } else {
  11.     document.getElementById("Rank2box").value="";
  12.     document.getElementById("fstname1").value="";
  13.     document.getElementById("lstname1").value="";
  14.    }
  15.         }
  16. }
  17.  
Use this code as you can see alert(checkobject("Rank2box")); alerts false meaning it couldn't find that object
but this is somewhat fixed
Newbie
 
Join Date: Feb 2007
Location: Canada
Posts: 23
#10: Mar 1 '07

re: Check that an element exists before doing somthing


Still not populating the second listbox it it gets created, ie the checkbox is NOT checked. So, kinda the same. ;)
Reply


Similar JavaScript / Ajax / DHTML bytes