Solved

Control if getElementById is returning something or not - Question

Posted on 2004-03-26
5
536 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
Comment Utility
hi mammaMia,

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

getElementById
NS 6+
IE 5+
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you test this:

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



0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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'…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now