Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function like IsObject()

Status
Not open for further replies.

BillLumbergh

Programmer
Aug 15, 2001
51
US
Is there a Javascript function that simply checks to see if what I am referring to is an object on a web form? For example, the Javascript would be doing some client side validation of a web form that may or may not contain a checkbox. If the checkbox exists, I want to run some validation on it. What I am essentially looking for is a Javascript function that will do this:

if (IsObject(document.form.chkMyCheckbox))
{
// means for field exists
do this;
}
else
{
form field does not exist, do nothing
}

What is the means of performing the IsObject() call? I'm guessing this is pretty basic; I just don't know it. The search I ran did not find anything that looked too promising.

Thanks.
 
if (document.form.chkMyCheckbox) should be sufficient.

Here's an example, try it first with the <!-- --> comments and then again without them.
Code:
<script language=javascript>

function checkForCheckbox() {
   alert((blahForm.blahCheckbox) ? "Checkbox exists" : "Checkbox does not exist")
}

</script>
<form name=blahForm>
<input type=button value='check for checkbox' onclick='checkForCheckbox()'><br>
<!-- <input type=checkbox name=blahCheckbox> -->
</form>

-kaht

banghead.gif
 
Dang, kaht beat me. Well if you wanted to use an IsObject function, this should do:
Code:
function IsObject(obj){
  return obj ? true : false;
}

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Adam, is it just me, or once you get used to using the "?" operator do you hardly ever use if statements anymore?

-kaht

banghead.gif
 
It's a nice little shortcut, but I still use if(condition){action} statements if there's no "else" or if I need to nest multiple "if" statements for readability.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
I think you're right there, kaht. Once you know the syntax of these things (for example, the order of the 4 directional elements used in CSS positioning - top, right, bottom, left - N,E,S,W), you use them more and more.

With regards to the ternary operator (?:) it certainly saves a few bytes, and in some cases makes code look a whole lot neater and more compact.

It also comes in handy when entering 20-line JavaScript competitions (shameless plug for my 3rd-place entry: ;o)

Dan
 
Dan,
Nice game for 20 lines of code, but I got to level 28 before I got bored. I only had to move left, then up for every level.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top