1
Good afternoon, I have a function in jquery that I have been improving since I discovered it in forums, it serves to create elements dynamically using jquery
/* função que insere e remove cadeia de inputs */
var inputnew = 0;
function new_input() {
inputnew++;
$('<li class="formation '+ inputnew +'">'
+ '<input placeholder="Modalidade" onkeyup="this.value=capitalize(this.value);" />'
+ '<input type="number" placeholder="Data" />'
+ '<input placeholder="Instituição" onkeyup="this.value=capitalize(this.value);" />'
+ '<span class="remove cursor_pointer">x</span>'
+ '</li>').prependTo($('section.duo div.academic_formation div.formations')); }
$('span.remove').live('click', function() {
if(inputnew > 1) {
$(this).parents('li').remove();
inputnew--; } });
$(document).ready(function() { new_input(); });
/* função que insere e remove cadeia de inputs */
I am facing the following problems, I am not experienced in jquery so I could not pass of this, I have already improved enough the function that I found and studied but I am with two problems, the main one is, apparently the function . live does not work in newer jquery, so I am required to use 1.8.3 for this function to work and the second problem is that, I need to create within the new_input function, all inputs and everything else that I wish to appear in the listed div, that is, it becomes difficult to edit and or improve and also make some functions work together, the problem of the version of jquery if it is complicated, or I don’t care much, to use in 1.8.3 without problems, but what I really want is for me to be able to scan the div that has the elements that I want to duplicate.
in case, I would have
<div class="formations">
<li class="formation">
<input placeholder="Modalidade" onkeyup="this.value=capitalize(this.value);" />
<input type="number" placeholder="Data" />
<input placeholder="Instituição" onkeyup="this.value=capitalize(this.value);" />
<span class="remove cursor_pointer">+</span>
</li>
</div>
html in the page, normally written and then a function that by clicking on an ADD for example, clone this excerpt below
Gee, it was very good, I didn’t remember the <template> and I’ve seen it working in ajax but I didn’t care about it, it was very good, just now do not delete the last element, IE, always stay 1, prevent the last one is deleted, I didn’t understand why it didn’t work since if > 1 is there, but it worked too well
– flourigh
found, have an additional inputnew++ there in your remove. removed and worked
– flourigh
@exact flourigh, had forgotten to take off. I corrected now.
– Sergio
Is there a way not to use the template? that is, take it straight from the <Section class="duo"> <div class="academic_formation"> <div class="formations">****</div> </div> </Section> without using getelementbyid?
– flourigh
@flourigh yes, use
var clone = document.querySelector('section.duo div.academic_formation div.formations').cloneNode(true);
– Sergio
Pow, it works, da to change yes, but something unexpected happened, it doubles, kkkkkkkkkkkkkkkkkkkkk, IE, when pressing 1x it displays 1 = 2 more when pressing again 8 and so 16, 32, IE, I think complicated, kkkkkkkk
– flourigh
@flourigh together
li.formation
to the selector:var clone = document.querySelector('section.duo div.academic_formation div.formations li.formation').cloneNode(true);
– Sergio
really, it worked the factor of duplicating but now it clones what is typed in the inputs, the bottom gets everything that was typed, in case it would have to send a "cleanrinput"? if there is a
– flourigh
I noticed that this way it doesn’t work because var clone_one = Document.querySelector('template#formations'). cloneNode(true); use query selector in template
– flourigh
@flourigh, the advantage of the template is that you have everything empty. If you want to copy what already exists you have to clean :)
– Sergio
yeah, but it was great, just a note, queryselector can’t read template#formations anyway?
– flourigh
@flourigh no, you have to use
.content
. But you can have normal HTML but withdisplay: none;
in CSS... there are many alternatives.– Sergio
thanks buddy, I did with a div on None display, it was great, next step will be to create a save to local
– flourigh