How to get id in jquery while many tds jquery [duplicate] - javascript

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.

Related

String escaping in generated html attribute jquery selectors [duplicate]

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 pass dynamically created element id to javascript function on button onclick. [duplicate]

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.

Remove attribute title from all HTML elements

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

How to target a div with a class and an inner p tag

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.

Replace all divs with value

I have the following code:
$('#select_albums').load(document.location.href + "&action=get_albums");
But this is only replacing only the first found div with the id #select_albums from DOM. How do I replace all divs with an id?
Every item in the DOM has a unique id. If you want to act on many divs at the same time use a class $(".myclass") or a tag $("div") selector.
Yes because by definition an element ID can only appear once on a page. If you have more elements use a class instead.
$('.select_albums').load(document.location.href + "&action=get_albums");
With
<div class="select_albums"></div>
<div class="select_albums"></div>
id's must be unique, try using a class instead such as $(".albums").load...
ids must be unique

Categories