dynamic - Cannot Read Property Value of Null - Javascript - Stack Overflow

Good afternoon everybody - I have a (probably) simplistic question that I can't seem to find the a

Good afternoon everybody - I have a (probably) simplistic question that I can't seem to find the answer to after searching fairly thoroughly. The error that I'm getting in chrome is "Uncaught TypeError: Cannot read property 'value' of null." on line 120 (copied below):

mercedes_txt = mercedes_txt + "<tr><td><button id=\"Mercedes Add Value\"
    onclick=\"addAttribute('Merceds-Benz', " + mercedes_counter + ", '" +
    document.getElementById('Mercedes-Benz Text').value + "')\">Add</button></td><td>
    <input type=\"text\" name=\"Mercedes-Benz Text\" id=\"Mercedes-Benz Text\" />
    </td></tr></table>";

I tried to divide the code up on multiple lines for readability purposes. If I take out "onclick" the code executes properly. Also, if I replace

'" + document.getElementById ('Mercedes-Benz Text').value + "'

with 'someValue', the code executes properly. So the problem must be that I'm requesting the value from an object that is dynamically created (it doesn't exist yet, but will once the code is applied in an innerHTML call). Sorry that I'm a novice but I appreciate everyone's help.

Good afternoon everybody - I have a (probably) simplistic question that I can't seem to find the answer to after searching fairly thoroughly. The error that I'm getting in chrome is "Uncaught TypeError: Cannot read property 'value' of null." on line 120 (copied below):

mercedes_txt = mercedes_txt + "<tr><td><button id=\"Mercedes Add Value\"
    onclick=\"addAttribute('Merceds-Benz', " + mercedes_counter + ", '" +
    document.getElementById('Mercedes-Benz Text').value + "')\">Add</button></td><td>
    <input type=\"text\" name=\"Mercedes-Benz Text\" id=\"Mercedes-Benz Text\" />
    </td></tr></table>";

I tried to divide the code up on multiple lines for readability purposes. If I take out "onclick" the code executes properly. Also, if I replace

'" + document.getElementById ('Mercedes-Benz Text').value + "'

with 'someValue', the code executes properly. So the problem must be that I'm requesting the value from an object that is dynamically created (it doesn't exist yet, but will once the code is applied in an innerHTML call). Sorry that I'm a novice but I appreciate everyone's help.

Share Improve this question asked Mar 5, 2013 at 19:28 Michael KoehlerMichael Koehler 531 gold badge1 silver badge5 bronze badges 2
  • 3 I don't think id attributes can contain spaces like that. So you're probably failing to get your element because of the invalid id. Also, you can't get elements that don't exist yet! – bfavaretto Commented Mar 5, 2013 at 19:29
  • 1 @bfavaretto correct. w3/TR/html5/dom.html#the-id-attribute – Matt Ball Commented Mar 5, 2013 at 19:32
Add a ment  | 

1 Answer 1

Reset to default 2

I see two errors:

  1. Trying to use an invalid id for your element. See What are valid values for the id attribute in HTML? for the id naming rules.

  2. You're trying to get the value of an element that you didn't add to the DOM yet.

I suggest getting rid of that inline onclick handler, and add by code. Something like this:

var addValueButton = document.getElementById('MercedesAddValue');
addValueButton.addEventListener('click', function(e) {
   // get live form values here by traversing the DOM
   // you may want to use jQuery...
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745322723a4622514.html

相关推荐

  • dynamic - Cannot Read Property Value of Null - Javascript - Stack Overflow

    Good afternoon everybody - I have a (probably) simplistic question that I can't seem to find the a

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信