jquery - Get and save all id start with using javascript -


i have structure this

<div id-"form-input">    <div id="form1"></div>    <div id="form2"></div>    <div id="form3"></div>    <div id="form4"></div> </div> 

i want number of form 1, 2, ... n. try using code id.

var num = new array(); var elems = $('div[id^=form]'); (var i=0; i<elems.length; i++) {    alert(elems[i]);    num.push(elems[i]); } alert(num); 

but returns [object htmldivelement]. wrong? how can number?

use map() method following.

var num = $('div[id^=form]').map(function() {      var id = this.id.split('form')[1];      if(!isnan(id))          return +id; // + convert string num  }).get();  alert(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id-"form-input">     <div id="form1"></div>     <div id="form2"></div>     <div id="form3"></div>     <div id="form4"></div>  </div>


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -