I want to compleately wipe the title attribute from all elements inside a html doc. From table, p, img, div etc..
Currently I do this:
$(document).ready(function(){
$("a").removeAttr("title");
$("img").removeAttr("title");
$("div").removeAttr("title");
// and so on
// and so on
// and so on
});
Is there a more elegant way to do this? Without selecting individual elements?
Use the attribute selector and select just the elements with the title attribute and not all elements.
$("[title]").removeAttr("title");
The all selector, *, selector should do the trick
$('*').removeAttr('title');
You can simply do this using All Selector (“*”):
$("*").removeAttr("title");
Without jQuery this would be:
Array.from(document.getElementsByTagName('*')).forEach(elem => elem.removeAttribute('title'));
or e.g. to remove the attribute only from specific tags, e.g. img:
Array.from(document.getElementsByTagName('img')).forEach(elem => elem.removeAttribute('title'));
Related
How to mix variables with selectors?
I have ID variable.
I want to select image with this id from div #one.
jQuery('#one img .id') is the selector. I've tried $('#one img .'+id) but doesn't work.
Edit: Based on your comment below, you would use this:
$('#one img.'+id)
In your question you have a space between img and the .class, I've simply removed that so you get img.className or img.'+className
With the introduction of template literals in ECMAScript 2015, you can also do
$(`#one img.${id}`)
This might just be a typo or you actually use the id variable in a class, but maybe it should be:
jQuery('#one img #'+id)
. is a class selector. Try changing that to a #:
$('#one img #'+id)
ID's should be unique in your HTML. So you should be able to select the ID directly without worrying about which DIV it is in. Since you mention the ID is attached to the img tag, then this should be enough.
$('#' + id);
Maybe you want to select img which set with specific class and save this class into id variable within div element with id='one'.
DEMO
In demo i select img element, clone it, and append to div id=one.
How to mix variables with selectors?
I have ID variable.
I want to select image with this id from div #one.
jQuery('#one img .id') is the selector. I've tried $('#one img .'+id) but doesn't work.
Edit: Based on your comment below, you would use this:
$('#one img.'+id)
In your question you have a space between img and the .class, I've simply removed that so you get img.className or img.'+className
With the introduction of template literals in ECMAScript 2015, you can also do
$(`#one img.${id}`)
This might just be a typo or you actually use the id variable in a class, but maybe it should be:
jQuery('#one img #'+id)
. is a class selector. Try changing that to a #:
$('#one img #'+id)
ID's should be unique in your HTML. So you should be able to select the ID directly without worrying about which DIV it is in. Since you mention the ID is attached to the img tag, then this should be enough.
$('#' + id);
Maybe you want to select img which set with specific class and save this class into id variable within div element with id='one'.
DEMO
In demo i select img element, clone it, and append to div id=one.
How to mix variables with selectors?
I have ID variable.
I want to select image with this id from div #one.
jQuery('#one img .id') is the selector. I've tried $('#one img .'+id) but doesn't work.
Edit: Based on your comment below, you would use this:
$('#one img.'+id)
In your question you have a space between img and the .class, I've simply removed that so you get img.className or img.'+className
With the introduction of template literals in ECMAScript 2015, you can also do
$(`#one img.${id}`)
This might just be a typo or you actually use the id variable in a class, but maybe it should be:
jQuery('#one img #'+id)
. is a class selector. Try changing that to a #:
$('#one img #'+id)
ID's should be unique in your HTML. So you should be able to select the ID directly without worrying about which DIV it is in. Since you mention the ID is attached to the img tag, then this should be enough.
$('#' + id);
Maybe you want to select img which set with specific class and save this class into id variable within div element with id='one'.
DEMO
In demo i select img element, clone it, and append to div id=one.
I would like to find all elements inside a container that have a certain data attribute set to 1 as well as all elements that don't have this attribute set at all.
The data attribute is as follows:
$("#element").data("activate")
It can have a value of 1 or 0. If an element doesn't have an "activate" data property set I want to treat it as a 0.
I have the following code at present:
$("#content").find("[data-activate='0']").off();
However I would also like to do something like this:
$("#content").find("all where data-activate NOT exists").off();
ie if an element doesn't have the attribute even set.
You can use :not:
$('#content :not([data-activate])').off();
Or filter():
$('#content div').filter(function() {
return !$(this).attr('data-activate');
}).off();
$("#content").find(":not([data-activate])").off();
TRY
$("#content div").map(function {
$(this).data("activate","1")
}
This will simply add data-activate = 1 to all div inside #content whether it is 0 or that attribute does not exist
You can use the two selector at once to select the element, separting them (selectors) by comma
:not() Selector.
Attribute Equals Selector.
$("#content [data-activate='0'], #content :not([data-activate])").off();
I have a structure that looks like this..
<div class="infobubble">
<p>
PLACE CONTENT HERE
</p>
</div>
How do I use jquery to target the in tags?
I tried this but did not work.
$("infobubble p").html('My Text');
Your code is wrong, you need to specify that you are looking for a class, like so
$(".infobubble p").html('My Text');
Your code $("infobubble p") would be looking for a tag element named infobubble which does not exist
$(".infobubble p").html('My Text');
in jQuery you need to add an '.' to select by class or '#' to select by id.
If no '.' or '#' is specified, jQuery will try to find an element by tag name... in your case it was trying to find <infobubble> which isn't an element.