• Resolved Insurteched

    (@jeyjoo)


    Hi,
    I have two forms on a page (teal background). Both have javascript events attached so for example when the user types in the input field:

    – non numeric characters are removed
    – the placeholder drifts above where the user is writing.

    This works on the second form, but the javascript events are not attached to the elements in the slider. I suspect this has something to do with dom loading order, but cannot work out exactly how to fix.
    Does anyone have a suggesiton?
    Thanks

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 19 total)
  • Hi,

    I see that there is Javascript error on the page:

    Uncaught TypeError: Cannot read property 'elements' of null.

    I’ve checked the jeyjoo.js code and found the following:

    – The previous error can be fixed if you added your code after the document is ready. So, you can add the validation code in the following block:

    jQuery(document).ready(function(){

    });

    – The form in the slider doesn’t have an ID. Do you expect it to have the ID “contact1” . This is just my guess from what I found in the code.

    Thread Starter Insurteched

    (@jeyjoo)

    Hi @mohammedeis, thankyou for the reply. This was a minor issue with variable declaration that is fixed. We also have added the form an ID.

    The problem remains that the form within the slideshow is not getting any Javascript events attached.

    Is it something to do with the parsing of the HTML. I notice in the page source code, rather than show:
    <form id=”contact1″
    we get:
    <form id="contact1"

    I see that the forms got valid ID attribute <form id=”contact1″. I was also able to concatenate the fields to the allFormElements array using the console.

    I think that the issue that the allFormElements doesn’t have the form contact1 elements.

    Please try to wrap your code in
    jQuery(document).ready(function(){

    });

    I think that the problem is that you are trying to add the fields before the slider gets initialized.

    Thread Starter Insurteched

    (@jeyjoo)

    I agree that the issue is loading order – we already tried jQuery(document).ready(function(){ and also window.onload but it doesn’t work.

    How can I get a handle on the slider javascript code in the functions.php. This way I can enqueue the slider javascript.

    Hi,

    Please try to execute the slide form JS work after metaslider/initialized event is being fired.

    Example:

    jQuery(document).on(‘metaslider/initialized’, function(e, identifier) {

    //Code that collects the form fields

    // Validate the form fields

    });

    Thread Starter Insurteched

    (@jeyjoo)

    Thank you @mohammedeisa – I can confirm that works!
    Nice job šŸ™‚

    Hi,

    I’m glad to hear that your issue has been fixed.

    Please let me know if you have any questions.

    Thread Starter Insurteched

    (@jeyjoo)

    Hi @mohammedeisa, actually I just noticed that this is still an issue. Following your solution above, the click events get assigned, but one the slideshow has done a full cycle of slides, the Javascript click events are no longer assigned the the input fields and buttons.
    Do you have any idea why this is and what we can do about it?
    Thanks

    Does the same happen when you use FlexSlider?

    Thread Starter Insurteched

    (@jeyjoo)

    Hi,
    I tried FlexSlider and R.slides, but the input fields were stripped out and did not render

    Hi,

    I need to reproduce this issue on my local installation.

    Please let me know how did you build this form?

    Thread Starter Insurteched

    (@jeyjoo)

    It is a simple HTML form with Javascript events attached.
    I think this problem occurs even with a simple html element. To test, I created on the first slide, a div: <div id=”abc”>test</div> (you can see the word “test”).

    With the first loading of the slide, the onclick event gets attached to the div id=”abc”. But once the slideshow goes to another slide, and back to this slide, the onlick event is lost.

    Hi,

    It worked with me using FlexSlider by doing the following:

    1- I downloaded jeyjoo.js and added it to my website by adding the following code to the end of the current theme functions.php:

    function load_my_script(){
        wp_register_script( 
            'my_script', 
            get_template_directory_uri() . '/jeyjoo.js', 
            array( 'jquery' )
        );
        wp_enqueue_script( 'my_script' );
    }
    add_action('wp_enqueue_scripts', 'load_my_script');

    2- I added the following line at the start of the jeyjoo.js
    var $= jQuery.noConflict();

    3- I added the following line of code to the theme functions.php to make MetaSlider allow adding HTML tags to the slides captions:
    add_filter('metaslider_filter_unsafe_html','__return_false');

    4- I added the form to the caption of the first slide of my slideshow.

    Please let me know if you need a video showing how the slider works.

    • This reply was modified 5 years, 2 months ago by mohammedeisa.
    Thread Starter Insurteched

    (@jeyjoo)

    Hi, I get the same result. The Javascript is attached until the slide changes. Once the slide has changed. the Javascript click events are lost.

    Hi,

    Then, there should be something wrong in your script or on your website generally.

    When I was testing the issue using your script, I removed all the code and kept only the form-related code. Can you try the same?

    If this didn’t work, then there should be some conflict on your website. To figure out that, please test the issue when only MetaSldier and the Twenty Twenty One theme are active.

Viewing 15 replies - 1 through 15 (of 19 total)

The topic ‘Attaching Javascript events to slideshow elements’ is closed to new replies.