Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This code not working in ASP.Net and give the error

Microsoft JScript runtime error: 'select' is null or not an object

my code is

var select = document.getElementsByTagName("Dd_Select_Month_Year")[0];
       select.onchange = function () {
           if (select.value == "2") {
               document.getElementsByTagName("txtDateFrom")[1].style.display = "inline";
               document.getElementsByTagName("txtDateTo")[1].style.display = "inline";
           } else {
               document.getElementsByTagName("txtDateFrom")[1].style.display = "none";
               document.getElementsByTagName("txtDateTO")[1].style.display = "none";
           }

       }
share|improve this question
I try too much search of this platform but i cant get the solution of this issue – Raheel 12 hours ago
1  
Try to replace all your 'getElementsByTagName' to 'getElementsById' in notepad or whatever. – vadz 12 hours ago
It would be helpful if you post markup as well. Also watch for txtDateTo vs. txtDateTO - some browsers are case-sensitive. – Ondrej Svejdar 11 hours ago

2 Answers

up vote 2 down vote accepted

Tag name is the HTML element's tag name. For a <select> element, the tag name is "select". Since there is no element with a tag "Dd_Select_Month_Year", getElementsByTagName() returns null.

Use

document.getElementsByTagName("select")[0];

Or if "Dd_Select_Month_Year" is your select's name attribute's value, that is,

<select name="Dd_Select_Month_Year">...</select>

use:

document.getElementsByName("Dd_Select_Month_Year")[0];
share|improve this answer
Thanks C.P.u1 and also Vadz for help and slove my problem but one thing that how we write select with asp.net tags – Raheel 11 hours ago

select as variable name should be avoided.

Reference:

  1. http://www.quackit.com/javascript/javascript_reserved_words.cfm
  2. http://www.javascripter.net/faq/reserved.htm

Try to use other word as variable name

var select2 = document.getElementsByTagName("Dd_Select_Month_Year")[0];
select2.onchange = function () {
    //Do whatever
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.