How to write JavaScript code to duplicate HTML elements? [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 6 years ago.
Improve this question
Suppose I have an HTML element (like a <div>). Is there a way to duplicate it after a click using JavaScript?
If yes, is it possible to change the content of the duplicated HTML element to something else?
EDIT: Yes, I was asking how to do it as well. Thanks to the guys who answered my question!

You can use Node.cloneNode() on original element, set Elment.innerHTML of cloned node.
a
<script>
var clone = document.querySelector("a").cloneNode();
clone.innerHTML = "b";
document.body.appendChild(clone);
</script>

See clone.
$('a').clone().html("something else");

Related

Is it a good practice to be a click function in the same tag as ngFor? Angular and JS [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a table and tr row.
Is it a good practice for my `Click` event to be the same tag as `*ngFor`? If it's not a good practice, what can I do to improve the code?
Yes, this is no problem, or we can use routerLink instead of onClick
[routerLink]="['./view', user.id]"
It's okay to have the click functionality on the ngFor tag itself. Because you're sending only the user and not the whole array.
Since u used async pipe, am assuming your array is an observable. Just make sure your observable doesn't cause any issues.

Change Text in IFrame without 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 6 years ago.
Improve this question
Weave: http://kodeweave.sourceforge.net/editor/#f6d9a54288f17fb68a8b234ec364dc41
I love JQuery, I do, but just for fun I'd like to learn how to write the following in pure/vanilla JS.
$("iframe").contents().find("<style>").html(cssEditor.value))
Something like this should work:
document
.getElementsByTagName('iframe')[0]
.contentDocument
.getElementsByTagName('style')[0]
.innerHTML = cssEditor.value;
Use the contentDocument property of the <iframe> DOM object to get the inner document.
You can then call normal methods on it like querySelector.

Invert everything on page except imges [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 8 years ago.
Improve this question
Hi I'm trying to invert everything on the page except for images? Any help will be appreciated. Thanks.
http://jsfiddle.net/nikita_turing/jVKw6/3/
Bookmarklet
If you can use jQuery, use the pseudo selector :not(img) to select all the elements except images, then set the CSS you want.
Something like
$(":not(img)").css({"-webkit-filter": "invert(100%)","-moz-filter": "invert(100%)","-o-filter": "invert(100%)","-ms-filter": "invert(100%)"});
There are can be two different solutions. Maybe not the best.
The first, Find all images
Then invert elements and invert images back to the first condition.
The second, using jQuery get all img elements and do the same work as in the previous idea.

jQuery, reuse method for many divs [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 8 years ago.
Improve this question
I have a CoffeeScript code that looks like this:
if $('#user_address_attributes_country').val() == ""
$('#user_address_attributes_country').val("PL")
Now I want to do the same thing for a few divs but without repetitions.
How can it be done by jQuery?
The reason you can't apply it multiple times is because your trying to apply it to an element which has an id. They are unique. Use classes and do this instead:
if ($('.user_address_attributes_country').val("")) {
$('.user_address_attributes_country').val("PL");
}
Simple demo: JsFiddle

Using CSS in JS [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 8 years ago.
Improve this question
I am trying to change CSS elements depending on situations used in javascript.
an example I had tried was:
if ('image'=="clicked")
{blahblahblah}
'image' would be my div id or div tag in CSS.
I know in Jquery I can use $('#image').click, but I don't think it Jquery will be good for my situation.
Consider document.querySelector. It's powerful, accepting any valid CSS selector. You can even start from another element, such as myElement.querySelector, to only get children of that element that match the selector. Very useful!
Just be aware that IE7 and below do not support this function.
Assuming your id as image
document.getElementById('image').onclick = function() {
alert("button was clicked");
}​;​

Categories