javascript - IE11 Unable to get property 'value' of undefined or null reference - Stack Overflow

it's seems that I'm having a frustrating problem and can't seem to find an answer.I

it's seems that I'm having a frustrating problem and can't seem to find an answer. I'm trying to get the value of the element in <td> tag. The id reaches the function but for some reason I can't get the value of it.

JS

function f(id)
  {
    console.log(id);
    expr=/ /gi;
    value = document.getElementById(id).value;
    value = value.replace(expr, "");

  //remaining code
  }

PHP

print "<td style=\"height:20px;\"><input $disbled type=\"text\" name=\"".$values[$i][0]."\" onChange=\"return f('".$values[$i][0]."')\" value=\"".$values[$i][1]."\" class=\"".$values[$i][2]."\"></td>\n"; 

Any help would be appreciated!

it's seems that I'm having a frustrating problem and can't seem to find an answer. I'm trying to get the value of the element in <td> tag. The id reaches the function but for some reason I can't get the value of it.

JS

function f(id)
  {
    console.log(id);
    expr=/ /gi;
    value = document.getElementById(id).value;
    value = value.replace(expr, "");

  //remaining code
  }

PHP

print "<td style=\"height:20px;\"><input $disbled type=\"text\" name=\"".$values[$i][0]."\" onChange=\"return f('".$values[$i][0]."')\" value=\"".$values[$i][1]."\" class=\"".$values[$i][2]."\"></td>\n"; 

Any help would be appreciated!

Share Improve this question asked Dec 23, 2015 at 12:37 CoffeMugCoffeMug 371 gold badge1 silver badge7 bronze badges 1
  • 1 Typo, you have a name not an id in your HTML, document.getElementById(id) returns null, calling value on that raises the error. – Alex K. Commented Dec 23, 2015 at 12:39
Add a ment  | 

2 Answers 2

Reset to default 3

A couple of problems there:

  1. Your element doesn't have an id at all

  2. Your code is falling prey to The Horror of Implicit Globals (that's a post on my anemic little blog).

Here's a fixed version:

function f(name)
{
    console.log(name);
    var expr=/ /gi;
    var value = document.querySelector('[name="' + name + '"]').value;
    value = value.replace(expr, "");
    //remaining code
}

#1 is fixed by using querySelector and an attribute selector selecting by name

#2 is fixed by declaring the local variables in f

You could also fix #1 by giving your element an id and sticking with getElementById.

The input you are trying to access has not ID property defined. You shall add it in order to access the input object via getElementById().

<input $disbled id=\"".$id."\" type=\"text\" name=\"".$values[$i][0]."\" onChange=\"return f('".$values[$i][0]."')\" value=\"".$values[$i][1]."\" class=\"".$values[$i][2]."\">

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信