etsted Posted September 15, 2014 Share Posted September 15, 2014 when i console.log the str variable it says "No suggestions", but when i try to use that statement in a if statement it doesnt work. var suggested_titels = ajax.responseText.split("S_E_C_R_E_T__S_I_G_N__!_|_!_|"); for(var i =0;i < suggested_titels.length; i++){ var space = document.createElement("BR"); var node = document.createElement("SPAN"); var textnode=document.createTextNode(suggested_titels[i]); var check = document.createTextNode(suggested_titels[0]); var str = document.createElement("DIV"); str.appendChild(check); node.appendChild(textnode); node.appendChild(space); sQ.appendChild(node); if(suggested_titels[0] == "No suggestions"){ sQ.innerHTML = ""; } } console.log(str); sQ is supposed to get empty when there is not suggestions in the DB Link to comment Share on other sites More sharing options...
dsonesuk Posted September 15, 2014 Share Posted September 15, 2014 So IF ajax return with only "No suggestions" which when split its index will be suggested_titels[0] you want sQ whatever that is? to be empty? but still create br, span and the rest for(var i =0;i < suggested_titels.length; i++){ //length will be 1 which will equal "No suggestions" var space = document.createElement("BR"); //still create br var node = document.createElement("SPAN"); // still create span var textnode=document.createTextNode(suggested_titels[i]); //still create text node of "No suggestions" as this will be the only text returned var check = document.createTextNode(suggested_titels[0]); // still ditto var str = document.createElement("DIV"); //still create div str.appendChild(check); // still append to div - check -> "No suggestions" node.appendChild(textnode); //still append to span - textnode -> "No suggestions" node.appendChild(space); //still append to span - space -> "<br>" sQ.appendChild(node); //still append to sQ? - span with textnode -> "No suggestions" + "<br>" if(suggested_titels[0] == "No suggestions"){ // if suggested_titels[0] matches "No suggestions" sQ.innerHTML = ""; // sQ whatever that is will be cleared. } } IS this right? Link to comment Share on other sites More sharing options...
etsted Posted September 15, 2014 Author Share Posted September 15, 2014 yes that is right. You can get a clear demonstration at my website widegrass.com. Search for "n" using my search bar, then add more "n" and see what happen Link to comment Share on other sites More sharing options...
davej Posted September 15, 2014 Share Posted September 15, 2014 Why are you starting a new thread? Isn't this a duplicate of this thread? http://w3schools.invisionzone.com/index.php?showtopic=51408&hl= In the code you have posted above it doesn't make any sense to have the "if" inside the for-loop. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now