473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Easy questions about if() and getElementById

Please forgive my ignorance. I've read a lot of reference and instructional materials but I need a couple of basic things clarified.

Suppose, in JavaScript, I do like so:

Expand|Select|Wrap|Line Numbers
  1. var a,b;
  2. a=document.getElementById("element");
  3. if(a){b= // . . . et cetera
I have seen people do things like this. My two questions:

1. What exactly does the variable a get filled with in line 2? It supposedly becomes a "reference" to the object with the specified ID, but what form does this "reference" take, specifically?

2. What's the purpose of the if() statement in the third line?

Thank you.
Dec 18 '08 #1
1 1330
Dormilich
8,658 Expert Mod 8TB
1. a holds an object (which in this case is an HTMLElement object)
2. the if statement checks whether the allocation of line 2 was successful, that is if a holds the element object or not (ie. there was no element having an id="element")

if (a) is true for any value of a besides null, 0, false and '' (empty string)

regards

note: try this:
Expand|Select|Wrap|Line Numbers
  1. alert(a);
Dec 18 '08 #2

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

Similar topics

7
by: reneecccwest | last post by:
Hello, Aren't they same conditional statment to show the resut? but somehow "switch" condtional stmt always gets the default value when I excute? what did I wrong? <script language="JavaScript">...
4
by: John | last post by:
I am a VB programmer and know nothing about Javascript. I have a Java Button in my asp page (It came with Visual Studio 6) and I want it to open a page called DBSearch.asp. I also want it to take...
4
by: Henry Su | last post by:
New to Javascript and have two questions, thanks in advance for any pointers!! 1) Is there a way to change the property of an eventHandler, for example -> <button id='btn123' onClick='abc()'>...
3
by: Jeff User | last post by:
Hi Sorry if this is kind of long, it is myhope that it is clear and staright forward. I am totaly new to javascript. I figured out this much. First, I wanted to clear the contents of a label...
1
by: Mr. Murad Jamal | last post by:
1) how to make a grid view cell scrollable ? 2) how to access that cell or any other control from Java Script code ? Thanx a million in advance !
7
by: LaughOutLoud | last post by:
Hi. Does anyone knows that how to retrieve variable on a link from javascript. e.g., here I have a link <a href="http://www.domain.com/index.html?id=1111" onClick="getid();">click me</a> <div...
2
by: windsorben | last post by:
I'd like to have an image appear after the student answers each short answer question correctly. I can't seem to get it to work properly. See code below. Thanks! <html> <head>...
9
by: thetechturf.com | last post by:
I have got a small problem getting my dropdown menu to work. I want to change the style with javascript. Here is the code in my HTML: <div id="navigation"> <ul> <li id="lnot"><a...
14
by: EJ | last post by:
I'm real new to javasscript. The task is to develop a web page which will allow users to customize a laptop ... show the price as they change options ... transfer the price and system description...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.