0

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";
           }

       }
3
  • I try too much search of this platform but i cant get the solution of this issue Commented Jun 13, 2013 at 11:21
  • 1
    Try to replace all your 'getElementsByTagName' to 'getElementsById' in notepad or whatever. Commented Jun 13, 2013 at 11:23
  • It would be helpful if you post markup as well. Also watch for txtDateTo vs. txtDateTO - some browsers are case-sensitive. Commented Jun 13, 2013 at 11:30

2 Answers 2

2

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];
Sign up to request clarification or add additional context in comments.

1 Comment

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
1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.