[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Control if getElementById is returning something or not - Question

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

649 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