-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
});
3
  • 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 page for help clarifying this question. Commented Oct 11, 2018 at 9:33
  • 1
    That isn't an error. It looks like panel is a DOM element, a <div> specifically. Commented Oct 11, 2018 at 9:34
  • Ok i will reformul my problem, sorry Commented Oct 11, 2018 at 9:36

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

7 Comments

and for loop my array, for display my data, how I can doing that plz ?
@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?
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 I updated my answer to include how to display the data in the HTML again.
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
|
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.