最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

textbox - Javascript: Uncaught TypeError: Cannot set property 'value' of null - Stack Overflow

programmeradmin1浏览0评论

I am trying to set a value of a textbox and I get this error... Please see my code:

this is my textbox

<input id="Text1" type="text" runat="server"/>

this is my drop down list

<select id="title" name="D1" onchange="Select()" >
                <option selected="selected"></option>
                <option value="1">Pilot</option>
                <option value="5">Engineer</option>                                    
             </select>

And that is my function

  <script type="text/javascript">


    function Select() {

        var ddl = document.getElementById("title");
        var selected = ddl.options[ddl.selectedIndex].value;
        document.getElementById("Text1").value = selected;
        alert(document.getElementById("Text1").value);
        if (selected == "5") {
            document.getElementById('divTechnician').style.visibility = "visible";
        } else {
            document.getElementById('divTechnician').style.visibility = "hidden";

        }            
    }
</script>

Error occurs when I try to assign value to this text box

document.getElementById("Text1").value = selected;

I am trying to set a value of a textbox and I get this error... Please see my code:

this is my textbox

<input id="Text1" type="text" runat="server"/>

this is my drop down list

<select id="title" name="D1" onchange="Select()" >
                <option selected="selected"></option>
                <option value="1">Pilot</option>
                <option value="5">Engineer</option>                                    
             </select>

And that is my function

  <script type="text/javascript">


    function Select() {

        var ddl = document.getElementById("title");
        var selected = ddl.options[ddl.selectedIndex].value;
        document.getElementById("Text1").value = selected;
        alert(document.getElementById("Text1").value);
        if (selected == "5") {
            document.getElementById('divTechnician').style.visibility = "visible";
        } else {
            document.getElementById('divTechnician').style.visibility = "hidden";

        }            
    }
</script>

Error occurs when I try to assign value to this text box

document.getElementById("Text1").value = selected;
Share Improve this question edited Jun 12, 2014 at 12:18 Matt K asked Jun 12, 2014 at 12:02 Matt KMatt K 391 gold badge2 silver badges10 bronze badges 9
  • 2 I don't see the divTechnician ID anywhere. – user2357112 Commented Jun 12, 2014 at 12:03
  • 1 That is irrelevant, divTechnician is not causing problems – Matt K Commented Jun 12, 2014 at 12:05
  • I don't get it it's working fine in jsbin – Camille Hodoul Commented Jun 12, 2014 at 12:08
  • 1 Please make sure that text box is present on the page at the moment when you're changing selector value. In Chrome developer tools, try executing document.getElementById("Text1"). – KIR Commented Jun 12, 2014 at 12:09
  • 1 There is no error in your javascript. – Matt Ellen Commented Jun 12, 2014 at 12:10
 |  Show 4 more ments

1 Answer 1

Reset to default 2

set clientidmode=static to this element

<input id="Text1" type="text" runat="server" clientIdMode="static"/>
发布评论

评论列表(0)

  1. 暂无评论