简体   繁体   中英

Show Hide Textbox using Javascript in ASP.Net

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

       }

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];

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM