469,329 Members | 1,460 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

Check that an element exists before doing somthing

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
9 35777
iam_clint
1,208 Expert 1GB
i am pretty sure you can just do
if (document.form1.Rank2) {
}
Mar 1 '07 #2
iam_clint
1,208 Expert 1GB
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
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
1,208 Expert 1GB
did you put that function inside another function?
Mar 1 '07 #5
did you put that function inside another function?
The "if" statment = yes.
The checkObj function itself = no.
Mar 1 '07 #6
iam_clint
1,208 Expert 1GB
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
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
1,208 Expert 1GB
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
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.

Similar topics

1 post views Thread by Paulos | last post: by
3 posts views Thread by michael | last post: by
23 posts views Thread by Randell D. | last post: by
1 post views Thread by Philip Wagenaar | last post: by
9 posts views Thread by Chris Ashley | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.