Select element with similar id using regex - jquery - javascript

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

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!

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/

Call element by id in javascript

I have a repeater and have a label with an icon inside it.
<strong><i id="iconProperties" class="icon-caret-right"></i> Properties</strong>
When i click a button the icon-caret-right must be turned to icon-caret-down. i have written the code as follows:
$('#iconProperties').removeClass('icon-caret-right').addClass('icon-caret-down');
When i use this code only the first row of the repeater is working. all other rows's icons are not changing. What is the mistake?
ASP.net generates unique ids for html elements and only one element can have id iconProperties that you used in selector. You can use Attribute Contains Selector [name*="value"] instead of id selector to get all the matching elements.
$('[id*=iconProperties]').removeClass('icon-caret-right').addClass('icon-caret-down');
If your ids have a similar name, you're probably after something like
$('[id^=iconProperties]').removeClass('icon-caret-right').addClass('icon-caret-down');
Which will update all items beginning with id "iconProperties".
It might be worth noting that it is common practice to use unique ids.
Try this:
$('#iconProperties').find('.icon-caret-right').replaceWith('.icon-caret-down');

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

Categories