javascript searching a json object -
i'm trying search value in json
<input type="text" id="test" size="21" maxlength="120"> <button onclick="zoek()" class="btn btn-info btn-block"> tijdelijke zoek knop </button>
i'm using input value , button call search function
function zoek() { var qeustion = document.getelementbyid("test").value; document.getelementbyid("accordion").innerhtml == ""; var text = '{ "faq" : [' + '{ "vraag":"john" , "antwoord":"doe" },' + '{ "vraag":"anna" , "antwoord":"smith" },' + '{ "vraag":"peter" , "antwoord":"jones" } ]}'; obj = json.parse(text); (i = 0; < text.length; i++) { if (obj.faq[i].vraag == qeustion) //(obj.faq[i].getstring("vraag").contains(question)) { document.getelementbyid("accordion").innerhtml += "<div class='panel panel-default'><div class='panel-heading' role='tab' id='heading" + + "'><h4 class='panel-title'><a data-toggle='collapse' data-parent='#accordion' href='#" + + "' aria-expanded='false' aria-controls='" + + "''>" + obj.faq[i].vraag + " </a></h4></div><div id='" + + "' class='panel-collapse collapse in' role='tabpanel' aria-labelledby='heading" + + "'><div class='panel-body'> " + obj.faq[i].antwoord + "</div></div></div> wowowowowowowowowwowowoow"; } else { document.getelementbyid("accordion").innerhtml = "no results found" } } }
and search function
so lets enter john goes straigt else , doesnt if statement though pretty sure kind of right
could give me pointers on searching in json object? there other way this?
please see jsfiddle attached demonstrating looking , show need - https://jsfiddle.net/vuenume2/1/
it essential have break
statement in loop.
without break
statement true value success gets overwritten false on next iteration, except last possible credentials, there no "next" iteration.
if (obj.faq[i].vraag == qeustion) { <!-- stuff --> break; } else { <!-- other stuff --> }
also, if haven't done need add div id accordion
html
<div id="accordion"></div>
Comments
Post a Comment