Solved

Control if getElementById is returning something or not - Question

Posted on 2004-03-26
5
543 Views
Last Modified: 2009-07-29
Hello!

I have a java script that disables a button if the user clicks on a hyperlink:

function disableButton() {
var theButton;
theButton = document.getElementById ('dg__ctl2_rb1');
theButton.disabled= true;
}

Is it possible do first check if the Button is on the page or not (sometimes the button isn’t on the page), because if I run the script and the button isn’t on the page I get a java-error. I don’t want to do this on the server-side, I mean if the button isent on the page I don’t write the script to the page when the client calls on it.

One more question: I know that Netscape 7.0 supports getElementById, but does any prior version support it? The same thing goes for IE 6.0, does any prior version support it?
0
Comment
Question by:mammaMia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
devic earned 125 total points
ID: 10685738
hi mammaMia,

simple check:
if(theButton)
theButton.disabled= true;

getElementById
NS 6+
IE 5+
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10685739
This works better:

function disableButton() {
  var elem = document.forms[0].elements;
  for(i=0;i<elem.length;i++){
     if(elem[i].name=='dg__ctl2_rb1'){
        elem[i].disabled= true;
     }
  }
}

0
 

Author Comment

by:mammaMia
ID: 10686334
Thanks for your replies!

I like the getElementByID approach better.

But what happens if I do like this and the browser doesn’t support getElementByID:

if(document.getElementById ('theButton')) {
var theButton;
theButton = document.getElementById ('theButton');
theButton.disabled= true;
}

Do I get an error or is nothing run inside the "if" and nothing happens with the button?
0
 
LVL 25

Expert Comment

by:devic
ID: 10686355
if browser do not support getElementByID, is not a browser ;)

if(document.getElementById)
{
if(document.getElementById ('theButton')) {
var theButton;
theButton = document.getElementById ('theButton');
theButton.disabled= true;
}
}
else
{
   alert("please install a browser ;)")
}
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10686380
Did you test this:

function disableButton() {
  var theButton = document.getElementById ('dg__ctl2_rb1');
  if(theButton){
    theButton.disabled= true;
  }
}



0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question