Skip to content Skip to sidebar Skip to footer

How To Let The User Input Text To Search Array Object, Then Output If There Is A Match? Pure Javascript Please

I am new to javascript, so please bare with me.Everyone on this site is so amazing to us newbies so thank you in advance.So here is where I am... I am building a really generic mus

Solution 1:

Quite a few problems, it should be artists not artist, output.value not output, in html should be id not name for textarea, you need to trim the user input of whitespace, I removed the alert from inside the loop (very annoying). I'm sure there was some other things but forgotten now. I still see a number of areas where this can be improved but I have not done anything about them. Here is my update. (it's funny, much of the code seems familiar somehow). Oh yes, be carefull when using innerHTML if you actually meant textContent, and when getting user input from an input element use value

http://jsfiddle.net/Xotic750/SSVSW/

Solution 2:

Use output.value instead of output in assigning statement. like this

output.value = formInput + " , " + album[i].title + " " + album[i].artist;

Also if you are expecting to show multiple list then you have to concatenate the results .like this

var output= document.getElementById("response");
var formInput = document.getElementById("formInput").innerHTML;
output.value = "";
  for (var i=0; i<album.length; i++){
       if (album[i].artist == formInput) {
           output.value += formInput + " , " + album[i].title + " " + album[i].artist;

       } else {
           alert("not found");
       }
  }

Assign id value "response" to your textarea tag since you are using getElementById.

And its better to have window.onload function for intialising all your functions

Post a Comment for "How To Let The User Input Text To Search Array Object, Then Output If There Is A Match? Pure Javascript Please"