-2

So, I have an input, I enter words for filter answers. My answers are my panel. I create an Array for register my answers.

var array = [];

But when I finish my loop, I want to innerHTML my datas who are in my Array. But I have a [object HTMLDivElement] because i push Object in Array. Ok my question is = How can I create an Array Object and push my panel in

var obj = {}

And How can I display them This code search in answer if the words exist

if (searchIndex > -1) {

If the answer exist , push the panel in my array and count how many answer he founds

    array.push(panel);
    countFound++;
}

When the loop is finish, exploit this data and display them

array1.forEach(function(element) {
   // I dont know how is the method to exploit all data one by one
});
Khyra
  • 37
  • 2
  • 8
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Wais Kamal Oct 11 '18 at 09:33
  • 1
    That isn't an error. It looks like `panel` is a DOM element, a
    specifically.
    – forgivenson Oct 11 '18 at 09:34
  • Ok i will reformul my problem, sorry – Khyra Oct 11 '18 at 09:36

2 Answers2

1

Depending on what your HTML looks like, you should use innerHTML attribute to get the contents of an element.

array.push(panel.innerHTML);

You get the object HTMLDivElement because that's what panel is.

If you want to iterate over the elements in array you can do it with a foor loop, alternative with a foreach loop, and to display it again in the HTML you once again need to use the innerHTML attribute.

text = "";
for (i=0; i < array.length; i++) {
    text += array[I];
}
document.getElementById("result").innerHTML = text;

And of you want to repeat the procedure you need to empty the array variable afterwards:

array = []; // Add this line after appending your data to the HTML.
            // Realistically after the for loop
Johan
  • 3,577
  • 1
  • 14
  • 28
  • and for loop my array, for display my data, how I can doing that plz ? – Khyra Oct 11 '18 at 10:04
  • @Khyra You're asking a separate question that requires some knowledge about your HTML. Do you want to display it on a web page? Do you have a specific HTML element that you want it to be shown in? – Johan Oct 11 '18 at 10:27
  • Yes I have a lot of questions, a lot of problems with my code because Im not an expert in all language. Hmm yes I want to display my data in a web page html. I will show you my code – Khyra Oct 11 '18 at 10:30
  • @Khyra I updated my answer to include how to display the data in the HTML again. – Johan Oct 11 '18 at 10:35
  • yes ok i see what you mean, compare with my version, Im in right or im totally out with my code for display the array ? Thx for take your time – Khyra Oct 11 '18 at 10:41
  • @Khyra It seems you need to empty the array afterwards. I have added this in my answer. Otherwise your implementation looks ok. Also - it's better to update your question instead of adding additional information in an answer. – Johan Oct 11 '18 at 10:47
  • Yes I understand the things, and I try to do it, but I dont know do it ... I put afterwards like you said, after my for an empty array but I dont know why the array is not empty – Khyra Oct 11 '18 at 10:55
1

For more Details go to adding-elements-to-object Here is how it works:

var element = {}, cart = [];
element.id = 1;
element.panel = document.getElementById("panel");

cart.push(element);

alert(cart[0].panel);
<div id="panel"></div>
Metatron5
  • 315
  • 2
  • 11