Difference between $("<div/>") and $("div") [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
What is the difference between $("<div />") vs $("div") ?

$('<div/>') : This creates a new div element. However this is not added to DOM tree unless you don't append it to any DOM element.
$('div') : This selects all the div element present on the page.
So, the code $('<div/>').addClass('myClass') will create a new div element and adds css class called myClass. And
$('div').addClass('myClass')
will select all the div element present on the page and adds css class myClass to them.

Related

How to select children of an element whose id is stored in a variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want select h2 and p tags which is inside a div whose id is saved in 'i' variable
so if i use document.querySelector("#i h2")
Its searching actual #i its not replacing it with the value of 'i'
i want to select document.querySelector("#(Value of i variable) h2")
Simple.
document.querySelector("#"+i+" h2");
or more fancifully:
document.querySelector(`#{i} h2`);
Simply concat the variable to the query string
document.querySelector("#" +i+ " h2")
Or even simply
document.querySelector(`#${i} h2`)

What is the best way to get and element in HTML by the text between the tags? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I find all elements with a specific string between the tags?
<foo>
<bar>sadfkwai foobar sapkdajda?</bar>
</foo>
Also, if after finding where the element is, if I could scope out until an element ´foo´
You can use document.getElementsByTagName to find <foo> elements.
Iterate over them, and remove if its content includes 2019:
for (const foo of document.getElementsByTagName('foo'))
if(foo.innerText.includes('2019'))
foo.parentNode.removeChild(foo)
If you have to do this only with the first element containing 2019, break the loop if it's found:
for (const foo of document.getElementsByTagName('foo'))
if(foo.innerText.includes('2019')) {
foo.parentNode.removeChild(foo)
break
}

Equivalent JQuery for Javascript id passing [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know the javascript to pass id when an event is triggered as below
<td><a id="applyChange_${(loop.index)}" onclick="javascript:applyChange(this);">
function applyChange(obj){
console.log(obj.id;) // this returns the id of the element
}
But how, the same can be written in jQuery?
Add class to your a tags and remove inline event binder
<td><a id="applyChange_${(loop.index)}" class="applyChange">
With this structure you can write Jquery selectors on classes, And on click of any element with the class applyChange you can gets that particular clicked elements id by using $(this).attr('id')
So the script would be like
$('.applyChange').on('click',function(){
console.log($(this).attr('id'));
});
I would do something like this if you just want to get the index:
<a class="applyChange" data-id="${(loop.index)}">
then setup the click event when the page loads (assuming these are dynamically created)
$(document).on('click', '.applyChange', function () { console.log($(this).data('id')) });

Making an array of all of the <p> tags font-sizes in my html [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I make an array of all of the p tags font-sizes in my html?
the p elements (no their parents)
You can use something like this ->
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
var para = document.getElementsByTagName("p");
fonts=[];
for(i=0;i<para.length;i++) {
var style = window.getComputedStyle(para[i], null);
fonts.push(style['font-size']);
}
console.log(fonts);
Demo: http://jsfiddle.net/1us76t0g/1/
Even if there are no inline styles defined - this way you could collect font-size values in array....

Turn a H3 tag into a link via jQuery? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I turn the following h3 tag into a clickable link (and set it href) via jQuery, while keeping the H3 mark and adding an tag.
My current code for example:
Click Here
There is a wrap function that makes this easy:
(function($){
var $hTag = $('.makemealink');
$hTag.wrap('');
})(jQuery);
This will wrap the h3 like:
<a href="#">
<h3 class="makemealink">Click Here</h3>
</a>

Categories