Skip Navigation

[Resuelto] jQuery «append» happens twice – only in view, not elsewhere

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I am using jQuery.each and jQuery.append to insert some content into the page after document ready, but for some reason append is happening twice.

Solution: Check your jQuery.each selector to confirm there is only one element in the DOM. In this case, there are two matching filters because the View is split into search form and results using separate shortcodes. The filters are actually in the DOM twice.

This support ticket is created hace 5 años, 10 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Este tema contiene 6 respuestas, tiene 3 mensajes.

Última actualización por Ido Angel hace 5 años, 10 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#1187335

hey,

take a look here:

enlace oculto

i have jQuery code to append text from checked input in a different place, like this:

$(".gender-group .checkbox input:checked").each(function() {
    var inigenderlabel= $(this).parent().text();
    $(".yoursearch .pickedgender").append("<span for="+$(this).attr("id")+">מין: "+inigenderlabel+"</span> ");
});

but it somehow appends the text twice on page load.
when i try the same code in JSfiddle it doesn't happen:

enlace oculto

any idea why?

thanks!

#1187348

You should use jQuery namespace instead of $, when working with WordPress.
https://toolset.com/documentation/user-guides/adding-custom-javascript-views/
https://toolset.com/documentation/user-guides/adding-custom-javascript-views/#achieving-great-results-with-little-coding-by-using-jquery

About the issue, I am not sure to understand.
I see you try to append a span with some text when a box is checked.
However, can you elaborate where on the page you link we should see this text?
I could not find it, either once or twice as stated in the problem report.

What is also possible is that you load the script twice, having added it to the Filter's Custom JS section in a front end event hook?

I would eventually also suggest, adding access details to a staging site copy of this website so we could take a closer look at how it's built and narrow down the issue.

#1187366
beda.JPG

Hey Beda,
Here's an image. The highlighted area on the right is the search filter input, the highlighted area at the top is the result.
Changing $ to jQuery didn't do anything, and I don't think the script is loaded twice, it's in the Fitler's Custom JS Section under

jQuery(document).ready(function( $ ){
#1187643
Untitled-1.jpg

Hey again. More errors which happen in jQuery but only on the site:

I'm trying to calculate the difference between the number in two fields.

Here, in JSFiddle, it works:

enlace oculto

And on the site - nope:

enlace oculto

it gives a crazy number in the results.

print screen attached.

any idea why this happens?

thanks!

#1187649

wait - the second issue (age difference) - my bad. got it to work.
but the append thing drives me nuts!

#1187744
Screen Shot 2019-01-20 at 4.31.16 PM.png

The gender group checkbox checked selector finds 2 results:

$(".gender-group .checkbox input:checked")

Then jQuery.each loops over both of those results, so the text is appended twice. It's not happening in the fiddle because there is only one result found. This is probably happening because you have split the search form and results up into separate shortcodes. When you do this, the search form is actually inserted in the DOM twice, but one is hidden. You can try adding a div with a specific CSS class around the shortcode that only shows the search form, then target just that search form.

$(".just-the-search-form .gender-group .checkbox input:checked")
#1187754

AMAZING! Wasn't aware of that - and that totally worked. As always.
Thanks Christian!