I have a asp.net Listview that is generating extra elements, good thing is the elements that I do want have a class name. How do I remove the s without the specific class that i need in jquery thanks!
Use the :not selector:
$("div:not([class])").remove();
Edit: jsfiddle.
class is just an attribute so you can select each one that doesn't have the attribute:
$('div:not([class])').remove()
$('div:not([class])').remove();
Related
I have below html and want to get the element based on 'natural' class. The reason is I get t dynamic classes after 'natural'
<coral-checkbox class="natural coral-Form-field coral3-Checkbox" ></coral-checkbox>
I am trying below code to get hide the element but it is not working.
$("coral-checkbox[class='.natural']").hide();
But it is working when I select entire class like below but I need to do with only 'natural'. Is this possible ?
$("coral-checkbox[class='.natural coral-Form-field coral3-Checkbox']").hide();
Use .classname to select based on any of the element's classed. When you use an attribute selector, it matches the entire attribute (unless you use modifiers like *=, but they're not appropriate here, either).
$("coral-checkbox.natural").hide();
Use the class the selector instead of the attribute selector:
$("coral-checkbox.natural").hide();
i need to add class in JS or JQ to all the tables without class in a site, some already as class so i need it to add the class only to the tables who doesn't have... please advice?
edit: i also need the class to be unique with number after the name of the class
$('table').each(function(i){
$(this).addClass('classname',i);
});
the addClass method only adds a class if it not exist in the selected element.
this works
$('table').addClass('classname');
If you need to add a class to only those tables without any classes (and not just all your tables), you can use:
$('table').each(function() {
if (!$(this).attr('class') || !$(this).attr('class').length) $(this).addClass('zzz')
})
You can check with not selector if particular class is not applied then apply it:
$("table:not(.classname)").addClass("classname");
Update:
But as others have mentioned you don't need to do that as addClass will take care of not adding class if it is there.
If you just want to target tables that don't have any class, you can use :not and the attribute selector
$('table:not([class!=""])').addClass('test');
that would be the same as:
$('table[class=""],table:not([class])').addClass('test');
I have elements in my page like
<div class="editableTxt" data-model-attr="name" data-model-id="123">some text</div>
Now how do I write a selector in jQuery based on the 2 custom attribute values.
So i want something like select element with data-model-attr="name" data-model-id="123"
I want to get a unique element. So I use the 2 attributes.
USe like this
$("[data-model-attr='name'][data-model-id='123']")
As you specified element and not div, have you simply tried:
$('[data-model-attr="name"][data-model-id="123"]');
JSFiddle: http://jsfiddle.net/TrueBlueAussie/x23BV/
for a div obviously just add div:
$('div[data-model-attr="name"][data-model-id="123"]');
Use:
$('div[data-model-attr="name"][data-model-id="123"]');
$('div[data-model-attr="name"][data-model-id="123"]')
But don't use it, it's very slow, set id or classes to this div.
Hi all. I am using jQuery and I want to replace the class on the DIV using jQuery.
I know that I can replace the class like this:
$('#div').removeClass('first').addClass('second');
But, I can use this only when I know the first class name. In my case, I do not know the first class name because it is dynamic.
You can use attr() to set the class, it will over write the previous class
$('#div').attr('class', 'second');
You can call removeClass without parameters
$('#div').removeClass().addClass('second');
When you call removeClass with no parameters this will remove all classes
$("#div").removeClass().addClass('second');
Try this :
$("#div").removeAttr('class');
$('#div').attr('class','second');
First line will remove the calss attribute, second will add a class with name second
The toggleClass() method toggles between adding and removing one or more class names from the selected elements.
This method checks each element for the specified class names. The class names are added if missing, and removed if already set - This creates a toggle effect.
$("#div").toggleClass(second,function(index,first),TRUE)
Try this:
$('#div').removeAttr('class').attr('class', 'second');
I would like to add an inline style to the last LI that is generated with the ASP:repeater control.
I can't add a css class, i need to some how count the last li with the class called:
class="tile lower-boxes icon_email"
If I've understood your question, then the following should work for you:
$(".tile.lower-boxes.icon_email:last").css("color", "#C00");
Obviously, that selector and the CSS method can be changed to your needs. You can also add a class to the element, which would be preferable:
$(".tile.lower-boxes.icon_email:last").addClass("foo");
More info on the :last selector.
Jquery has a last function that will let you choose it functionally.
$('li.tile').last()
or you can use the :last selector
$('li.tile:last')
Edit: Fix Bad link
we can also use jQuery :last Selector..
e.g:
$("li:last").css('background-color', 'yellow');
I know the question is quite old but thought it might be helpful for others ..