How to capture a jquery element and leave it changed in a new variable without changing in the view?

Asked

Viewed 48 times

2

For example, I want to take the DOM element, and then change, without it changing the behavior in the view:

//aqui pego o conteúdo da html
var content = $('.content').get(0);
//aqui eu reescrevo, porém ocorre que o $('.content') também está sendo alterado...???
 var new_content = $(content).removeClass('ql-align-justify');

I want only the variable to be modified.

  • 3

    You want to create a clone, that’s it? var content = $('.content').clone().get()

  • If I clone, the element will not be duplicated in the view?

  • This is not possible to accomplish. You need to clone the object as @Sergio commented or create a "hidden" element and change it.

  • @Ivanferrer no, stay in the variable until you add it to DOM/View. That’s what you want?

  • It worked here Sergio, thank you.

  • @Ivanferrer great! I left an answer for if others also look for the same problem.

Show 1 more comment

1 answer

1


You can create a clone, which generates an identical element but only contained in the variable. You can then change it and then put it in the DOM / View when needed.

var content = $('.content').clone().get();
$(content).removeClass('ql-align-justify');
// e mais tarde 
$(body).append(content);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.