How to apply functions in an array with javascript?

0

Suppose we have the following function in javascript:

var modal=$("div[class*=div_modal]");
//Accedo al primer elemento del array
var modal2 = modal[0];
//quiero obtener el primer div
modal2.children('div').eq(0);
console.log(modal2);

But I get the following error:

modal2.children is not a function

Why does this happen? How do I apply the children?

    
asked by Susje 29.10.2017 в 15:48
source

2 answers

1

Assuming that you have an HTML similar to the example I give below and that you are using jQuery, I would proceed as follows:

var modal = $("div.div_modal");
var modal2 = modal.eq(0);
console.log(modal2);

// Y para obtener el contenido
console.log(modal2.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
   <div class="div_modal">1</div>
   <div class="div_modal">2</div>
   <div class="div_modal">3</div>
</div>
    
answered by 29.10.2017 в 17:42
1

As you wrote in a comment @amenadiel, that is a HTML object which does not have the methods of jQuery and if it is possible to make them have their methods, you just have to do something like this:

//Tú código
var modal=$("div[class*=div_modal]"),
      //Usamos el selector de jQuery para poder usar sus métodos
      modal2 = $(modal[0]).children('div').eq(0);

console.log(modal2);

I do not know much about jQuery and I really do not like to use it, but you just have to select HTMLElement to be able to use their methods.

I hope and serve you.

    
answered by 29.10.2017 в 18:29