Reputation: 339
I have this dynamically formed divs that appear inside an already existing div.
<div id="onlineList">
<div id="ajaxChat_u_109" name="username1" data-gender="8">whatever1</div>
<div id="ajaxChat_u_109" name="username2" data-gender="8">whatever2</div>
<div id="ajaxChat_u_109" name="username3" data-gender="8">whatever3</div>
<div id="ajaxChat_u_109" name="username4" data-gender="8">whatever4</div>
<div id="ajaxChat_u_109" name="username5" data-gender="8">whatever5</div>
<div id="ajaxChat_u_109" name="username6" data-gender="8">whatever6</div>
<div id="ajaxChat_u_109" name="username7" data-gender="8">whatever7</div>
</div>
What i want to make is a jquery function that removes the divs that do not contain the string passed by an inputfield.
<input type="text" id="searchuser" value="" />
<input type="button" id="gosearchuser" value=">" />
I've put together a Jfidle http://jsfiddle.net/38Ndw/1/ where you can see jquery that's obviously not working.
Upvotes: 3
Views: 1016
Reputation: 93571
This answer assumes a case-insensitive, partial match search of the name
attributes is required (now confirmed from comments):
For your specific example problem, I kind of like slideUp()
and slideDown()
as they move the remaining elements correctly and leave them in place so the filter can be changed again and again:
It uses a filter
function which is basically a callback that gets passed each matching element in turn. If the function returns true (to include) or false (to exclude) this changes the resulting list. slideUp()
(was remove()
) is then applied only to the resulting matches.
This one also includes the case-insensitive matching you wanted.
$("#gosearchuser").on("click", function (event) {
var searchuser = $('#searchuser').val().toLowerCase();
$('#onlineList').children('div').slideDown().filter(function(){
return $(this).attr('name').toLowerCase().indexOf(searchuser) == -1;
}).slideUp();
});
This is also slightly more efficient as it only searches for $('#searchuser')
once (which is preferable to searching the same element/value, over and over, for every matching element).
Old versions below
It uses a filter function which is bascially a callback that gets passed each matching element in turn. If it returns true (to include) or false (to exclude) this changes the resulting list. remove()
is then applied only to the result matches.
$("#gosearchuser").on("click", function (event) {
$('#onlineList').children('div').filter(function(){
return $(this).attr('name').indexOf($('#searchuser').val()) == -1;
}).remove();
});
Ideally the filter should be specific enough to not catch any other elements. In this case it matches just the child div
s of #onlineList
but it could just as easily be:
all children of the list (fastest):
$('#onlineList').children().filter...
or match just those with a name=
attribute:
$('#onlineList').children('[name]').filter...
or just div
s with a name=
attribute:
$('#onlineList').children('div[name]').filter...
which can also be combined with the parent id selector:
$('#onlineList > div').filter...
$('#onlineList > div[name]').filter...
based on your comments the duplicate ids are just in the sample data, so I have not addressed those
Mr7-itsurdeveloper (below) made an interesting suggestion, that you simply hide the element rather that remove them. You can then repeat the button over and over. Instead of classes I have used hide()
and show()
:
$("#gosearchuser").on("click", function (event) {
$('#onlineList').children('div').show().filter(function(){
return $(this).attr('name').indexOf($('#searchuser').val()) == -1;
}).hide();
});
fadeIn()
and fadeOut()
best$("#gosearchuser").on("click", function (event) {
$('#onlineList').children('div').fadeIn().filter(function(){
return $(this).attr('name').indexOf($('#searchuser').val()) == -1;
}).fadeOut();
});
You normally just need to call toLowerCase
on both strings before comparing the strings:
$("#gosearchuser").on("click", function (event) {
var searchuser = $('#searchuser').val().toLowerCase();
$('#onlineList').children('div').filter(function(){
return $(this).attr('name').toLowerCase().indexOf(searchuser) == -1;
}).remove();
});
Upvotes: 3
Reputation: 339
Ok,
With many thanks to you all i've chosen TrueBlueAussie to have provided the answer i needed.
This is the code now that also makes the search caseinsensitive,
$("#gosearchuser").on("click", function (event) {
$('#onlineList').children('div').fadeIn().filter(function(){
return $(this).attr('name').toLowerCase().indexOf($('#searchuser').val().toLowerCase()) == -1;
}).fadeOut();
}); });
Upvotes: 0
Reputation: 1631
use following code ,this will work
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#gosearchuser").on("click", function (event) {
peoplediv = $('#onlineList').children('div');
searchparam = $('#searchuser').val();
$(peoplediv).each(function () {
if ($(this).attr('name') == searchparam ){
alert('jaaaaaaaaaaaaaaaaa');
$(peoplediv).not(this).remove();
}
else {
return;
}
});
}); });
</script>
Upvotes: 0
Reputation: 74738
Try with this using .filter()
:
$("#gosearchuser").on("click", function (event) {
$('#onlineList').children('div').filter(function(){
return $(this).attr('name').indexOf($('#searchuser').val()) !== -1;
}).remove();
});
You have a invalid html markup because of duplicacy of ids again and again.
Upvotes: 4
Reputation: 8171
Try this:
$("#gosearchuser").on("click", function (event) {
$('#onlineList').children('div').filter(function(){
return $(this).attr('name').toLowerCase().indexOf($('#searchuser').val().toLowerCase()) == -1;
}).remove();
});
Upvotes: 1
Reputation: 28513
Try this:
$("#gosearchuser").on("click", function (event) {
var inputVal = $('#searchuser').val();
$('#onlineList').children().each(function(){
if($(this).attr('name').indexOf(inputVal)==-1)
(this).remove();
});
});
Upvotes: 1
Reputation: 4275
Jquery
$("#gosearchuser").on("click", function (event) {
var $searchparam = $('#searchuser').val();
$('div[name="' + $searchparam + '"]').hide();
});
Upvotes: 0
Reputation: 62488
If you are trying to check for attribute name
of div then you have to do like this:
$("#gosearchuser").on("click", function (event) {
var $searchparam = $('#searchuser').val();
$('div#onlineList div').each(function () {
if ($(this).attr('name') == $searchparam ){
}
else {
this.remove();
}
});
});
If you are trying to check for text of div then you have to do like this:
$("#gosearchuser").on("click", function (event) {
var $searchparam = $('#searchuser').val();
$('div#onlineList div').each(function () {
if ($(this).text().trim() === $searchparam ){
} else {
this.remove();
}
});
});
Upvotes: 0