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
Post a Comment