This question already has answers here:
How to find if div with specific id exists in jQuery?
(10 answers)
Closed 7 years ago.
I have some DIVs with IDs like 'abc_chosen', 'danger_chosen', etc.
I would like to delete all DIVs when their IDs contain the string '_chosen'.
How can I do that?
Try this:
$('div[id$="_chosen"]').remove();
$ here will check if _chosen is at the end of id.
Thanks to #evolutionxbox:
If you want to check if id contain _chosen anywhere(not at the end)
$('[id*="_chosen"]').remove();
Docs: https://api.jquery.com/attribute-ends-with-selector/
Related
This question already has answers here:
Javascript getElementById based on a partial string
(9 answers)
Closed 3 years ago.
I just want to know to how to use custom varible with javascript.suppose we have 5 id with name zawsz,baws2,tawsx,paws4,vawsa and we want to execute single DOM command to all these element whose id is define here.
document.getElementById("-aws-").onclick=function(){}
Here -aws- define all the id define above.(-) can be replace with any char/int value;
You could use the following code:
(The following code will select all elements of which the id includes aws.
I have tested this code and it works: https://jsfiddle.net/5042woqz/)
document.querySelectorAll('*[id*="aws"]').forEach(function(item) {
item.onclick=function() {
console.log('click!');
};
});
Items will now be an array containing all your aws- items.
If you have further questions, just let me know.
P.S.: You could achieve the same thing really easily with jquery.
You can use document.querySelectorAll for this:
document.querySelectorAll('[id^="aws"]')
That will select all elements where the id attribute starts with (^=) "aws".
This question already has answers here:
How do I concatenate a string with a variable?
(5 answers)
Closed 3 years ago.
This is basic ,
I can show or hide a class with :
$('.blink').show();
but I would like to do the same with a parameter so
var a = "blink";
$(a).show();
will not work obviously and I couldn't find the keywords to look for this in google.
You need to append a dot when you use that variable so that the $ will consider it as a class selector:
var a = "blink";
$('.'+a).show();
This question already has answers here:
How to add a class to a given element?
(28 answers)
Closed 7 years ago.
Is there a way to add a second class to an HTML tag using javascript? For example, say you had the element <p class="a"> and you wanted to add class="b". If you used document.getElementByClassName("a").class = "b" that would remove class "a". Is there a way to have both in the same element?
document.getElementsByClassName("a")[0].className += " b"
First of all, the method is named getElementsByClassName, plural. You need to refer to a specific element in the collection that it returns, via a zero-based index.
And you need the space before b here, so that you don’t end up with ab, but a b.
Try this one here:
document.getElementByClassName("a").className += " b"
Got it from here: How do I add a class to a given element?
This question already has answers here:
jQuery - How to select value by attribute name starts with
(4 answers)
Closed 7 years ago.
I want to remove all attributes with name starting data-val-range.
I.e. from the following element I wanna remove the matching attributes:
<input data-val-range-min="*" data-val-range-max="$" data-val-range="hallelujah"/>
Is this possible?
Using this answer, You can just iterate through the attributes, and remove them according to the name...
Your javascript should be something like
$("input").each(function() {
var theElem=this;
$.each(this.attributes,function() {
if(this.specified) {
if(this.name.indexOf("data-val-range")>-1) {
console.log("removing ",this.name);
$(theElem).removeAttr(this.name);
}
}
})
});
Here is a jsfiddle https://jsfiddle.net/dkusds1s/
This question already has answers here:
jQuery how to find an element based on a data-attribute value?
(9 answers)
Closed 8 years ago.
Similar to the question here , I need to get an element based on its data-id.
The problem I encounter is that I want to retrieve a an a element with a certain data-id using a variable.
lets consider the code below:
var item_id = 12345;
if I want to get the element that has the data-id with the value of item_id, would I do the following ?
$('[data-id=item_id]')
This does not work, and I am kind of stuck on how I can get around this.
You can concatenate the variable using +:
$('[data-id=' + item_id + ']')