I have create code to show/hide data from DB, it's works but as i am new to jquery I would like to know am I on good way? thank you.
HTML:
<li class="devInfo">
<span class="sn_table"><? echo $sn; ?></span>
<span class="last_edit_table"><? echo $last_change; ?></span>
<span class="comment_table"><? echo $comment; ?></span>
<span class="model_table"><? echo $model_name; ?></span>
<a class="more" data-name="<? echo $id; ?>" href="">+</a>
<div class="details"></div>
</li>
Jquery:
$(function () {
$(".more").live("click", function () {
var onMe =$(this);
var detailsConteiner = $(onMe).next('div');
var forLoader = $(onMe).parent("li");
if ($(onMe).text() == "+") {
$(devInfo).children("span").css({"text-indent":"", "text-overflow":""});
$(forLoader).fadeIn(400).prepend('<img src="/images/css/loader_small.gif" style="margin-bottom:.25em; margin-left:1.25em; vertical-align:middle;">');
$(onMe).text("-")
$(devInfo).css("background", "");
$(forLoader).css("background", "#d6e5f4");
$(onMe).siblings("span").css({"text-indent":"1000px", "text-overflow":"clip"});
$(detailsConteiner).empty();
$(details).slideUp(1000);
var value = $(onMe).attr("data-name");
var dataString = 'deviceId=' + value;
$.ajax({
type: "POST",
url: "helpers/getDetails.php",
data: dataString,
cache: false,
success: function (html) {
$(detailsConteiner).prepend(html).slideDown(500);
$(forLoader).children("img").fadeOut(600);
}
});
return false;
}
else {
$(details).slideUp(700);
$(onMe).text("+");
$(forLoader).css("background", "");
$(onMe).siblings("span").css({"text-indent":"", "text-overflow":""});
return false;
}
});
});
ajax response:
<dl class="details">
<dd>Id: <? echo $id; ?> </dd>
<dd>Serial: <? echo $sn; ?> </dd>
<dd>Model: <? echo $model_name; ?> </dd>
<dd contenteditable data-name="comment">Comment:<? echo $comment; ?></dd>
<dd>Last Change<? echo date('d.m.Y', strtotime($last_change)); ?> </dd>
</dl>
EDIT:
CSS:
.devInfo img {
margin-bottom:.25em;
margin-left:1.25em;
vertical-align:middle;
margin-left:25px;
}
li.expanded {
background-color: #d6e5f4;
}
li.expanded span {
text-indent: -1000px;
text-overflow:clip;
}
JS:
*function showDetails() // is not needed as the function insertDetails is doing job *added some code:
function hideRestOfLI() {
$(".details").slideUp(1000);
}
function expand() {
hideRestOfLI();
$(".devInfo").removeClass(ExpandClass);
$(".more").text(ColapsedChar);
$devInfo.addClass(ExpandClass);
$moreLink.text(ExpandedChar);
}
completed: hideLoading // complete: hideLoading