?
Solved

Control if getElementById is returning something or not - Question

Posted on 2004-03-26
5
Medium Priority
?
544 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

764 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