Javascript search with parser
up vote
0
down vote
favorite
I want to get this code working with my parser.
function search() {
var input, filter, table, tr, td, i;
input = document.getElementById("input");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
parser:
function load() {
data = data[0];
var uno = document.getElementById('data');
for(var i = 0; i < data.list.length; i++)
{
var content = uno.innerHTML;
content += "<div class='grid-item'><div class='inside' id='item" + i + "'><h2 id='contents" + i + "' class='pl-content-title' href=./index.html#" + data.list[i].hash + ">" + data.list[i].title + "</h2><div class='collapsing'><br><table id='mytable'>";
for(var e = 0; e < data.list[i].content.length; e++)
{
content += "<tr><td class='tabledata'><a class='pl-content' href=./index.html#" + data.list[i].content[e].hash + ">" + data.list[i].content[e].title + "</a></td></tr>";
}
uno.innerHTML = content + "</table></div></div></div>";
}
html:
<input class="pl-search" type="text" placeholder="Suche" id="input" onkeyup="search()">
If I copy the table directly into my html it works but it doesnt if the table is built by my parser script.
The error message from the browser console is:
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
Any ideas on this?
javascript html
|
show 4 more comments
up vote
0
down vote
favorite
I want to get this code working with my parser.
function search() {
var input, filter, table, tr, td, i;
input = document.getElementById("input");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
parser:
function load() {
data = data[0];
var uno = document.getElementById('data');
for(var i = 0; i < data.list.length; i++)
{
var content = uno.innerHTML;
content += "<div class='grid-item'><div class='inside' id='item" + i + "'><h2 id='contents" + i + "' class='pl-content-title' href=./index.html#" + data.list[i].hash + ">" + data.list[i].title + "</h2><div class='collapsing'><br><table id='mytable'>";
for(var e = 0; e < data.list[i].content.length; e++)
{
content += "<tr><td class='tabledata'><a class='pl-content' href=./index.html#" + data.list[i].content[e].hash + ">" + data.list[i].content[e].title + "</a></td></tr>";
}
uno.innerHTML = content + "</table></div></div></div>";
}
html:
<input class="pl-search" type="text" placeholder="Suche" id="input" onkeyup="search()">
If I copy the table directly into my html it works but it doesnt if the table is built by my parser script.
The error message from the browser console is:
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
Any ideas on this?
javascript html
What is your parser script? Do you have a table with idmyTable
? How are we supposed to help you if you don't show the relevant parts of your code?
– Federico klez Culloca
Nov 9 at 8:08
sry I thought this was irrelevant. Ill post my parser script
– T. Tom
Nov 9 at 8:10
It's just because you mention it. I'm not really sure it's relevant either. If not, why did you mention it and why is it important in the context of the question? I'd say the table is more important, actually, since the problem seems to be thatdocument.getElementById("myTable")
returnsnull
.
– Federico klez Culloca
Nov 9 at 8:11
but it works if the same table is built in the html. the parser is in the post now.
– T. Tom
Nov 9 at 8:13
1
Seeing your code, you're giving the table andid
ofmytable
, while the script is trying to getmyTable
.id
s are case-sensitive.
– Federico klez Culloca
Nov 9 at 8:14
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to get this code working with my parser.
function search() {
var input, filter, table, tr, td, i;
input = document.getElementById("input");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
parser:
function load() {
data = data[0];
var uno = document.getElementById('data');
for(var i = 0; i < data.list.length; i++)
{
var content = uno.innerHTML;
content += "<div class='grid-item'><div class='inside' id='item" + i + "'><h2 id='contents" + i + "' class='pl-content-title' href=./index.html#" + data.list[i].hash + ">" + data.list[i].title + "</h2><div class='collapsing'><br><table id='mytable'>";
for(var e = 0; e < data.list[i].content.length; e++)
{
content += "<tr><td class='tabledata'><a class='pl-content' href=./index.html#" + data.list[i].content[e].hash + ">" + data.list[i].content[e].title + "</a></td></tr>";
}
uno.innerHTML = content + "</table></div></div></div>";
}
html:
<input class="pl-search" type="text" placeholder="Suche" id="input" onkeyup="search()">
If I copy the table directly into my html it works but it doesnt if the table is built by my parser script.
The error message from the browser console is:
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
Any ideas on this?
javascript html
I want to get this code working with my parser.
function search() {
var input, filter, table, tr, td, i;
input = document.getElementById("input");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
parser:
function load() {
data = data[0];
var uno = document.getElementById('data');
for(var i = 0; i < data.list.length; i++)
{
var content = uno.innerHTML;
content += "<div class='grid-item'><div class='inside' id='item" + i + "'><h2 id='contents" + i + "' class='pl-content-title' href=./index.html#" + data.list[i].hash + ">" + data.list[i].title + "</h2><div class='collapsing'><br><table id='mytable'>";
for(var e = 0; e < data.list[i].content.length; e++)
{
content += "<tr><td class='tabledata'><a class='pl-content' href=./index.html#" + data.list[i].content[e].hash + ">" + data.list[i].content[e].title + "</a></td></tr>";
}
uno.innerHTML = content + "</table></div></div></div>";
}
html:
<input class="pl-search" type="text" placeholder="Suche" id="input" onkeyup="search()">
If I copy the table directly into my html it works but it doesnt if the table is built by my parser script.
The error message from the browser console is:
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
Any ideas on this?
javascript html
javascript html
edited Nov 9 at 8:13
asked Nov 9 at 8:06
T. Tom
125
125
What is your parser script? Do you have a table with idmyTable
? How are we supposed to help you if you don't show the relevant parts of your code?
– Federico klez Culloca
Nov 9 at 8:08
sry I thought this was irrelevant. Ill post my parser script
– T. Tom
Nov 9 at 8:10
It's just because you mention it. I'm not really sure it's relevant either. If not, why did you mention it and why is it important in the context of the question? I'd say the table is more important, actually, since the problem seems to be thatdocument.getElementById("myTable")
returnsnull
.
– Federico klez Culloca
Nov 9 at 8:11
but it works if the same table is built in the html. the parser is in the post now.
– T. Tom
Nov 9 at 8:13
1
Seeing your code, you're giving the table andid
ofmytable
, while the script is trying to getmyTable
.id
s are case-sensitive.
– Federico klez Culloca
Nov 9 at 8:14
|
show 4 more comments
What is your parser script? Do you have a table with idmyTable
? How are we supposed to help you if you don't show the relevant parts of your code?
– Federico klez Culloca
Nov 9 at 8:08
sry I thought this was irrelevant. Ill post my parser script
– T. Tom
Nov 9 at 8:10
It's just because you mention it. I'm not really sure it's relevant either. If not, why did you mention it and why is it important in the context of the question? I'd say the table is more important, actually, since the problem seems to be thatdocument.getElementById("myTable")
returnsnull
.
– Federico klez Culloca
Nov 9 at 8:11
but it works if the same table is built in the html. the parser is in the post now.
– T. Tom
Nov 9 at 8:13
1
Seeing your code, you're giving the table andid
ofmytable
, while the script is trying to getmyTable
.id
s are case-sensitive.
– Federico klez Culloca
Nov 9 at 8:14
What is your parser script? Do you have a table with id
myTable
? How are we supposed to help you if you don't show the relevant parts of your code?– Federico klez Culloca
Nov 9 at 8:08
What is your parser script? Do you have a table with id
myTable
? How are we supposed to help you if you don't show the relevant parts of your code?– Federico klez Culloca
Nov 9 at 8:08
sry I thought this was irrelevant. Ill post my parser script
– T. Tom
Nov 9 at 8:10
sry I thought this was irrelevant. Ill post my parser script
– T. Tom
Nov 9 at 8:10
It's just because you mention it. I'm not really sure it's relevant either. If not, why did you mention it and why is it important in the context of the question? I'd say the table is more important, actually, since the problem seems to be that
document.getElementById("myTable")
returns null
.– Federico klez Culloca
Nov 9 at 8:11
It's just because you mention it. I'm not really sure it's relevant either. If not, why did you mention it and why is it important in the context of the question? I'd say the table is more important, actually, since the problem seems to be that
document.getElementById("myTable")
returns null
.– Federico klez Culloca
Nov 9 at 8:11
but it works if the same table is built in the html. the parser is in the post now.
– T. Tom
Nov 9 at 8:13
but it works if the same table is built in the html. the parser is in the post now.
– T. Tom
Nov 9 at 8:13
1
1
Seeing your code, you're giving the table and
id
of mytable
, while the script is trying to get myTable
. id
s are case-sensitive.– Federico klez Culloca
Nov 9 at 8:14
Seeing your code, you're giving the table and
id
of mytable
, while the script is trying to get myTable
. id
s are case-sensitive.– Federico klez Culloca
Nov 9 at 8:14
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
<table id='mytable'>
should be
<table id='myTable'>
This typo caused table = document.getElementById("myTable");
to return null.
In a static example like this, you just fix the typo.
When coding a dynamic function, prevent errors by using a fallback
tr = table ? table.getElementsByTagName("tr") : null;
and so on with tr
when you evaluate the for
. (declare k)
for (i = 0, k= tr ? tr.length : 0; i < k; i++)
2
Or, you know, just fix the typo.
– Federico klez Culloca
Nov 9 at 8:16
Sure, just making the code more robust in case.
– Attersson
Nov 9 at 8:17
This is not more robust, this will just crash further down the line when you'll try to gettr.length
and generally just stop working.
– Federico klez Culloca
Nov 9 at 8:17
using too much fallbacks is not good because it can make the code hard to debug. Let it throw that error so that you know that the name is incorrect on first sight. (that is why you need to test your own code first). That is more OK when working with dynamic names.
– KarelG
Nov 9 at 8:18
I already mentioned but just edited it anyway according to suggestions.
– Attersson
Nov 9 at 8:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
<table id='mytable'>
should be
<table id='myTable'>
This typo caused table = document.getElementById("myTable");
to return null.
In a static example like this, you just fix the typo.
When coding a dynamic function, prevent errors by using a fallback
tr = table ? table.getElementsByTagName("tr") : null;
and so on with tr
when you evaluate the for
. (declare k)
for (i = 0, k= tr ? tr.length : 0; i < k; i++)
2
Or, you know, just fix the typo.
– Federico klez Culloca
Nov 9 at 8:16
Sure, just making the code more robust in case.
– Attersson
Nov 9 at 8:17
This is not more robust, this will just crash further down the line when you'll try to gettr.length
and generally just stop working.
– Federico klez Culloca
Nov 9 at 8:17
using too much fallbacks is not good because it can make the code hard to debug. Let it throw that error so that you know that the name is incorrect on first sight. (that is why you need to test your own code first). That is more OK when working with dynamic names.
– KarelG
Nov 9 at 8:18
I already mentioned but just edited it anyway according to suggestions.
– Attersson
Nov 9 at 8:21
add a comment |
up vote
1
down vote
accepted
<table id='mytable'>
should be
<table id='myTable'>
This typo caused table = document.getElementById("myTable");
to return null.
In a static example like this, you just fix the typo.
When coding a dynamic function, prevent errors by using a fallback
tr = table ? table.getElementsByTagName("tr") : null;
and so on with tr
when you evaluate the for
. (declare k)
for (i = 0, k= tr ? tr.length : 0; i < k; i++)
2
Or, you know, just fix the typo.
– Federico klez Culloca
Nov 9 at 8:16
Sure, just making the code more robust in case.
– Attersson
Nov 9 at 8:17
This is not more robust, this will just crash further down the line when you'll try to gettr.length
and generally just stop working.
– Federico klez Culloca
Nov 9 at 8:17
using too much fallbacks is not good because it can make the code hard to debug. Let it throw that error so that you know that the name is incorrect on first sight. (that is why you need to test your own code first). That is more OK when working with dynamic names.
– KarelG
Nov 9 at 8:18
I already mentioned but just edited it anyway according to suggestions.
– Attersson
Nov 9 at 8:21
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
<table id='mytable'>
should be
<table id='myTable'>
This typo caused table = document.getElementById("myTable");
to return null.
In a static example like this, you just fix the typo.
When coding a dynamic function, prevent errors by using a fallback
tr = table ? table.getElementsByTagName("tr") : null;
and so on with tr
when you evaluate the for
. (declare k)
for (i = 0, k= tr ? tr.length : 0; i < k; i++)
<table id='mytable'>
should be
<table id='myTable'>
This typo caused table = document.getElementById("myTable");
to return null.
In a static example like this, you just fix the typo.
When coding a dynamic function, prevent errors by using a fallback
tr = table ? table.getElementsByTagName("tr") : null;
and so on with tr
when you evaluate the for
. (declare k)
for (i = 0, k= tr ? tr.length : 0; i < k; i++)
edited Nov 9 at 8:20
answered Nov 9 at 8:15
Attersson
3,0981423
3,0981423
2
Or, you know, just fix the typo.
– Federico klez Culloca
Nov 9 at 8:16
Sure, just making the code more robust in case.
– Attersson
Nov 9 at 8:17
This is not more robust, this will just crash further down the line when you'll try to gettr.length
and generally just stop working.
– Federico klez Culloca
Nov 9 at 8:17
using too much fallbacks is not good because it can make the code hard to debug. Let it throw that error so that you know that the name is incorrect on first sight. (that is why you need to test your own code first). That is more OK when working with dynamic names.
– KarelG
Nov 9 at 8:18
I already mentioned but just edited it anyway according to suggestions.
– Attersson
Nov 9 at 8:21
add a comment |
2
Or, you know, just fix the typo.
– Federico klez Culloca
Nov 9 at 8:16
Sure, just making the code more robust in case.
– Attersson
Nov 9 at 8:17
This is not more robust, this will just crash further down the line when you'll try to gettr.length
and generally just stop working.
– Federico klez Culloca
Nov 9 at 8:17
using too much fallbacks is not good because it can make the code hard to debug. Let it throw that error so that you know that the name is incorrect on first sight. (that is why you need to test your own code first). That is more OK when working with dynamic names.
– KarelG
Nov 9 at 8:18
I already mentioned but just edited it anyway according to suggestions.
– Attersson
Nov 9 at 8:21
2
2
Or, you know, just fix the typo.
– Federico klez Culloca
Nov 9 at 8:16
Or, you know, just fix the typo.
– Federico klez Culloca
Nov 9 at 8:16
Sure, just making the code more robust in case.
– Attersson
Nov 9 at 8:17
Sure, just making the code more robust in case.
– Attersson
Nov 9 at 8:17
This is not more robust, this will just crash further down the line when you'll try to get
tr.length
and generally just stop working.– Federico klez Culloca
Nov 9 at 8:17
This is not more robust, this will just crash further down the line when you'll try to get
tr.length
and generally just stop working.– Federico klez Culloca
Nov 9 at 8:17
using too much fallbacks is not good because it can make the code hard to debug. Let it throw that error so that you know that the name is incorrect on first sight. (that is why you need to test your own code first). That is more OK when working with dynamic names.
– KarelG
Nov 9 at 8:18
using too much fallbacks is not good because it can make the code hard to debug. Let it throw that error so that you know that the name is incorrect on first sight. (that is why you need to test your own code first). That is more OK when working with dynamic names.
– KarelG
Nov 9 at 8:18
I already mentioned but just edited it anyway according to suggestions.
– Attersson
Nov 9 at 8:21
I already mentioned but just edited it anyway according to suggestions.
– Attersson
Nov 9 at 8:21
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221901%2fjavascript-search-with-parser%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is your parser script? Do you have a table with id
myTable
? How are we supposed to help you if you don't show the relevant parts of your code?– Federico klez Culloca
Nov 9 at 8:08
sry I thought this was irrelevant. Ill post my parser script
– T. Tom
Nov 9 at 8:10
It's just because you mention it. I'm not really sure it's relevant either. If not, why did you mention it and why is it important in the context of the question? I'd say the table is more important, actually, since the problem seems to be that
document.getElementById("myTable")
returnsnull
.– Federico klez Culloca
Nov 9 at 8:11
but it works if the same table is built in the html. the parser is in the post now.
– T. Tom
Nov 9 at 8:13
1
Seeing your code, you're giving the table and
id
ofmytable
, while the script is trying to getmyTable
.id
s are case-sensitive.– Federico klez Culloca
Nov 9 at 8:14