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

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("");

Related

How to select all elements that ends with certain id

Edit: let me reformulate this question
How to select all elements that ends with certain id
Ex:
<input id="person">
<span id="span_person"></span>
<input id="person_001">
My problem is, when I select with jQuery like
$("[id*=person]");
all inputs with similar id's are returning but in this case I just want to select the first two. I want to select all elements that the id ends with "person". Is there any way?
I use a framework that generates html code, so I can't change the id of the elements because it's based on the attribute name on the framework.
Use the $ operand for CSS selectors
[attribute$=value]
$("[id$='person']");
eg. Selects every element whose attribute value ends with value
An id is supposed to be unique. You should use a class to group elements you want to style with similar CSS, something like '.person'. Refer to: https://www.w3schools.com/html/html_id.asp
ID is only! You can't set ID for 2 tags!
I should change your span's id!

Select element with similar id using regex - jquery

An example of the element I need to select.
$("#edit_profile_form_profile_person_affiliations_2_user");
Id is same but a number of the element will change.
I want to select all elements. Is this is possible with the id of elements?
$("[id^=edit_profile_form_profile_person_affiliations_]")
It will match id's that start with "edit_profile_form_profile_person_affiliations_".
As answer above,
$("[id^=edit_profile_form_profile_person_affiliations_]")
this is good to check if ID starts with
and this to check for end if ID
$("[id$=_user]");

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/

jQuery "not contains" selector

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");

Categories