jQuery "not contains" selector - javascript

The :contains() jQuery selector allows you to match find elements that contain a specified string of text. What I want to do seems related: I'm providing the user a "filter" text box that they can type in, and I have a set of list items.
I want to have all list items that do not contain the text the user entered in the textbox be hidden as they type.
I can listen for the keyup event on the textbox, but I'm not sure how to do two things:
"Invert" the :contains() selector results--I want to select elements that don't match, and hide them.
Make the matching case sensitive.
It's occurred to me that I could use .filter( function(index) ), but I'm wondering if I'm overthinking this--is there a way to accomplish this already with the selectors/functions built-in to jQuery?

Assuming your user's text is stored in a variable called userString:
$(':not(:contains('+ userString +'))').hide();
will hide all of the elements not containing that string.
Edit:
If you have control over the elements in the list items, you could transform the user's text when you store it in the userString var.

Let say that you want the list of all <td> that dont contains string 'Dinesh'.(assuming Dinesh is inside name Attribute)
$('tableId > tbody > tr > td').not("[name*='Dinesh']")

Use this:
$("div:not(:contains('John'))").css("text-decoration", "underline");

Related

Selecting element with specific class and value

How to select element with class MyClass and value MyValue, but without using each?
I tried something like:
$(".MyClass").find("[value='MyValue']")
$(".MyClass[value='MyValue']")
This is example: http://jsfiddle.net/HQaG5/
It works if i use hard coded value for select element.
You want :contains() :
$( ".MyClass:contains('MyValue')" )
Take a look here: jquery find element by text
You can use the :contains selector to find elements containing text.
But if you want to match an exact string then .filter is the better option
If the element is an input and you want to search after property value you can use .filter()
$('.MyClass').filter(function(){
return this.value=="MyValue";
});
DEMO
If you are looking to get the select box element that has the specified value selected (which seems to be the case based on your fiddle), then you can use this…
$(".MyClass option:selected[value='MyValue']").parent()
DEMO
However, I would question why you want to do this, as it seems kind of backwards.

how to get part of textbox id in jquery

I have one textbox .the id is account_0__abc.the id will dynamically generted one.my question is how to select the id ending with __abc textboxes in a whole form using jquery?
Try to use attribute ends with selector,
$('[id$="__abc"]')
There are various selectors in jQuery to identify elements based on a part of their id or names. You can specify the element type as well.
Here's an example:
$('input[id$="__abc"]')
This will grab <input> elements with id ending with __abc. Be careful though, if you got multiple ones that match this criteria, you'll end up with a collection. You can iterate through the collection and do stuff to them with a .each() like so:
$('input[id$="__abc"]').each(function(){
// magic
});
If you want to make it more specific such as start with account_ and end with __abc then you can use:
$('[id^="account_"][id$="__abc"]')
https://api.jquery.com/attribute-starts-with-selector/
https://api.jquery.com/attribute-ends-with-selector/

clear all textboxes which all have id contains a particular string?

i have to clear all text box have id contains f_evoucher. Is it possible to clear it in a single line using jquery or javascript?
i have already tried like below
document.getElementById("f_evoucher").value="";
document.getElementById("f_evoucher_2").value="";
document.getElementById("f_evoucher_3").value="";
document.getElementById("f_evoucher_4").value="";
you can use the attribute starts with selector
$('[id^="f_evoucher"]').val('')
Note: It is very costly to use the attribute selector alone, so combine it with other selectors.
Ex: all the target elements are of type input then you can
$('input[id^="f_evoucher"]').val('')
Use attribute start selector:It matches the starting string
$("[id^='f_evoucher']").val("");

:contains incorrectly selecting child elements

Currently I'm trying to select a link element with the jQuery :contains selector. This works when the contents of the link is just text. but it seems that when the element contains other HTML elements the :contains selector selects a child element instead. Example
HTML:
<b> two</b> this not bold <b>This</b> is a bold Link
from that html, I'm trying to select the link using this selector
jQuery:
var selector = "a:contains('<b> two</b> this not bold <b>This</b> is a bold Link')";
var returnObj = $(selector);
Instead of getting one returned object (the link), jQuery returns three objects:
the first bold element
the text this is not bold
the second bold element
the problem isn't the single quotes within the contains(), as I've tried with and without them.
This is just a simplified example of what I'm trying to do. In reality, I'm dynamically creating selectors based off of a link object a user clicks. I then store that selector in a database for use later (for my app to display content related to that link). Since I can get the contents of the link, I figured I'd just use a:contents() if the link doesn't have an id.
based off of these pages, I seem to have my syntax right:
http://docs.jquery.com/Tutorials:How_to_Get_Anything_You_Want_2
http://api.jquery.com/contains-selector/
Thoughts on how to get the link object returned? Thanks!
hope this isn't too stupid a question, I'm new to JS and jQuery.
As mentioned, :contains() is meant to select by text content only, not inner HTML.
If you must match the a element based on that text, strip out the <b> tags:
var selector = "a:contains(' two this not bold This is a bold Link')";
Otherwise, see if you can simplify this selection by using a contextual selector (e.g. select based on its surrounding elements, parents, siblings, etc), or assign it a class and select by that class instead.
On a side note, I'd consider this yet another jQuery bug (could be a parsing error in Sizzle). In your situation, :contains() is not supposed to return or create any elements; it's supposed to return no matches simply because the selector doesn't match your a element. I suspect what it's doing instead is treating the <b></b> tags as new elements, and creating them on the fly along with your a element, which is wrong because the tags are inside the argument string and meant to be taken literally...
First of all your selector text does not match the actual text in your html.
The selector includes the this not bold which is not present in the html.
Most importantly the :contains works with the text only.. so you should check for
$("a:contains('two this not bold This is a bold Link')");
It is a very inefficient way though, and you should better add a class to the elements you want to target and use that for targeting..

jQuery: Get one of a set of selected elements

There are several elements that are selected by $(".foo"). $(".foo").text() returns the text of each element concatenated together. I just want the text of one element. What is the best way to do this?
$(".foo")[0].text() fails.
You want to use .eq(0), like this:
$(".foo").eq(0).text()
When you do $(".foo")[0] or $(".foo").get(0) you're getting the DOM Element, not the jQuery object, .eq() will get the jQuery object, which has the .text() method.
Normally using the # selector syntax selects one element by id attribute value. Do you have more than one element with the same id attribute value? If so, then you need to correct your HTML. id attribute values should be unique within a document.
The items in the jQuery array always return the dom elements (not the jQuery wrapped elements). You could do something like:
$($("#foo")[0]).text()

Categories