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 anid
in your HTML,document.getElementById(id)
returnsnull
, callingvalue
on that raises the error. – Alex K. Commented Dec 23, 2015 at 12:39
2 Answers
Reset to default 3A couple of problems there:
Your element doesn't have an
id
at allYour 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条)