Checking data of all same class elements
Posted
by
Tiffani
on Stack Overflow
See other posts from Stack Overflow
or by Tiffani
Published on 2012-09-25T15:30:57Z
Indexed on
2012/09/25
15:37 UTC
Read the original article
Hit count: 336
JavaScript
|jQuery
I need the code to check the data-name value of all instances of .account-select. Right now it just checks the first .account-select element and not any subsequent ones.
The function right now is on click of an element such as John Smith, it checks the data-name of the .account-select lis. If the data-names are the same, it does not create a new li with the John Smith data. If no data-names are equal to John Smith, then it adds an li with John Smith.
This is the JS-Fiddle I made for it so you can see what I am referring to: http://jsfiddle.net/rsxavior/vDCNy/22/ Any help would be greatly appreciated.
This is the Jquery Code I am using right now.
$('.account').click(function () {
var acc = $(this).data("name");
var sel = $('.account-select').data("name");
if (acc === sel) {
}
else {
$('.account-hidden-li').append('<li class="account-select" data-name="'+ $(this).data("name") +'">' + $(this).data("name") + '<a class="close bcn-close" data-dismiss="alert" href="#">×</a></li>');
}
});
And the HTML:
<ul>
<li><a class="account" data-name="All" href="#">All</a></li>
<li><a class="account" data-name="John Smith" href="#">John Smith</a></li>
</ul>
<ul class="account-hidden-li">
<ul>
© Stack Overflow or respective owner