By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

Check that an element exists before doing somthing

P: 23
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
Mar 1 '07 #1
Share this Question
Share on Google+
9 Replies


iam_clint
Expert 100+
P: 1,208
i am pretty sure you can just do
if (document.form1.Rank2) {
}
Mar 1 '07 #2

iam_clint
Expert 100+
P: 1,208
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.
Mar 1 '07 #3

P: 23
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
Mar 1 '07 #4

iam_clint
Expert 100+
P: 1,208
did you put that function inside another function?
Mar 1 '07 #5

P: 23
did you put that function inside another function?
The "if" statment = yes.
The checkObj function itself = no.
Mar 1 '07 #6

iam_clint
Expert 100+
P: 1,208
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
Mar 1 '07 #7

P: 23
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.
Mar 1 '07 #8

iam_clint
Expert 100+
P: 1,208
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
Mar 1 '07 #9

P: 23
Still not populating the second listbox it it gets created, ie the checkbox is NOT checked. So, kinda the same. ;)
Mar 1 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.