Nitish Kumar Nitish Kumar - 6 months ago 15
Javascript Question

how to get values in an array in jQuery

I'm having a following set of html codes:

<div class="carousel-inner" id="nitsslider">
<div class="item active" style="background-image: url(../nits-img/global/templates/himu/slider/slide3.jpg)">
<div class="carousel-caption">
<div>
<h2 class="heading animated bounceInDown">'Himu' Onepage HTML Template</h2>
<p class="animated bounceInUp">Fully Professional one page template</p>
<a class="btn btn-default slider-btn animated fadeIn" href="#">Get Started</a>
</div>
</div>
</div>
<div class="item" style="background-image: url(../nits-img/global/templates/himu/slider/slide2.jpg)">
<div class="carousel-caption">
<div>
<h2 class="heading animated bounceInDown">Get All in Onepage</h2>
<p class="animated bounceInUp">Everything is outstanding </p> <a class="btn btn-default slider-btn animated fadeIn" href="#">Get Started</a>
</div>
</div>
</div>
<div class="item" style="background-image: url(../nits-img/global/templates/himu/slider/slide1.jpg)">
<div class="carousel-caption">
<div>
<h2 class="heading animated bounceInRight">Fully Responsive Template</h2>
<p class="animated bounceInLeft">100% Responsive HTML template</p>
<a class="btn btn-default slider-btn animated bounceInUp" href="#">Get Started</a>
</div>
</div>
</div>
</div>


I want the text values of h2 tags, p tags and a tag into an array, like I've the background image illustrated below:

$('#nitsslider > .item').each(function (i) {
var sliderimage = [];
var sliderheading = [];
var sliderpara = [];
var sliderbutton = [];
sliderimage[i] = $(this).css('background-image').replace(/^url\(['"](.+)['"]\)/, '$1');
sliderheading[i] = $('.item > h2').text();
console.log(sliderimage[i]);
console.log(sliderheading[i]);
});


I want to use these values to appending into the code.

Please help me out guys. Thanks

Answer

Use iteration context this along with find selector to find child element in it:

sliderheading[i] = $(this).find('h2').text();