Solved

Control if getElementById is returning something or not - Question

Posted on 2004-03-26
5
539 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
  • 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

809 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