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?










share|improve this question
























  • 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") 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






  • 1




    Seeing your code, you're giving the table and id of mytable, while the script is trying to get myTable. ids are case-sensitive.
    – Federico klez Culloca
    Nov 9 at 8:14















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?










share|improve this question
























  • 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") 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






  • 1




    Seeing your code, you're giving the table and id of mytable, while the script is trying to get myTable. ids are case-sensitive.
    – Federico klez Culloca
    Nov 9 at 8:14













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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") 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






  • 1




    Seeing your code, you're giving the table and id of mytable, while the script is trying to get myTable. ids 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










  • 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












  • 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 of mytable, while the script is trying to get myTable. ids 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. ids 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. ids are case-sensitive.
– Federico klez Culloca
Nov 9 at 8:14












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++) 





share|improve this answer



















  • 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 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












  • I already mentioned but just edited it anyway according to suggestions.
    – Attersson
    Nov 9 at 8:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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++) 





share|improve this answer



















  • 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 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












  • I already mentioned but just edited it anyway according to suggestions.
    – Attersson
    Nov 9 at 8:21















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++) 





share|improve this answer



















  • 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 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












  • I already mentioned but just edited it anyway according to suggestions.
    – Attersson
    Nov 9 at 8:21













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++) 





share|improve this answer














<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++) 






share|improve this answer














share|improve this answer



share|improve this answer








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 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












  • I already mentioned but just edited it anyway according to suggestions.
    – Attersson
    Nov 9 at 8:21














  • 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 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












  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff