user6924814 user6924814 - 1 month ago 7
CSS Question

Onclick open and close div error

I am creating a web services which generates array. I want div open and close on this. I have a div which is on clicking should open another div but in my case when i load my page all divs are opened by default. All i want div to be remained close only on clicking it should open. I want on click of

class="innr-spl3 kmore"
,
ul class="details"
to be opened and again on clicking it should close.

PHP

<?php if(count($json['resultList']) > 0): ?>
<?php
foreach ($json['resultList'] as $key=>$value) {
if($json["resultList"][$key]["interestRateMin"] == $json["resultList"][$key]["interestRateMax"]){
$interest = $json["resultList"][$key]["interestRateMin"];
}
else{
$interest = $json["resultList"][$key]["interestRateMin"].' - '.$json["resultList"][$key]["interestRateMax"];

}

if($json["resultList"][$key]["financingPercentageMin"] == $json["resultList"][$key]["financingPercentageMax"]){
$financing = $json["resultList"][$key]["financingPercentageMin"];
}
else{
$financing = $json["resultList"][$key]["financingPercentageMin"].' - '.$json["resultList"][$key]["financingPercentageMax"];

}

echo '
<div class="cr-content">
<div class="bank-rates">
<ul>
<li>
<div class="innr-spl" style="padding: 5px 0;">
<span style="display:block;"><img src="'.$json["resultList"][$key]["imageUrl"].'"></span>
<span class="hide">'.$json["resultList"][$key]["bankName"].'</span>
</div>
</li>

<li><div class="innr-spl2">' . $interest . '%' . '</div></li>
<li><div class="innr-spl2">' . $financing . '</div></li>
<li class="hide"><div class="innr-spl3 kmore" id="'.$key.'"><i class="fa fa-chevron-circle-down" aria-hidden="true"></i>View Datails</div> </li>
</ul>
<div style="clear:both"></div>

<ul class="details" id="ban_'.$key.'">
<li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Maximum Loan Amount : '.$json["resultList"][$key]["maxLoanAmt"].'</li>
<li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Max Tenure (In Years) : '.$json["resultList"][$key]["repaymentTenureInYears"].'</li>
<li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Interest Rate Type : '.$json["resultList"][$key]["rateType"].'</li>
</ul>

</div>
</div>

';
$i++;
}

?>
<?php endif;?>


JS

$(document).ready(function(){

$('.kmore').click(function(){
var name = this.id;
//alert(name);
$("#ban_" + name).slideToggle("fast");
});

});

Answer

Two things needed in your case:-

  1. <ul class="details" id="ban_'.$key.'" style = "display:none;">

  2. var name = $(this).attr('id'); // use attr()

Note:- rest of the code seems fine to me.

Comments