Get the page title within a Javascript dynamic variable

Asked

Viewed 85 times

1

Hello!

There’s a form I needed to put in the name as dynamic. Originally, this name was just registerformpopup. Now I put as registerformpopup + the title of the page that varies according to the page on which the user completed the form.

That worked perfectly.

<form class="<?php if ( get_theme_mod( 'thim_auto_login', true ) ) {
    echo 'auto_login';
} 
?>" name="<?php echo 'registerformpopup' . get_the_title() ?>" 
    action="<?php echo esc_url( site_url( 'wp-login.php?action=register', 'login_post' ) ); ?>" 
    method="post" novalidate="novalidate">

<?php wp_nonce_field( 'ajax_register_nonce', 'register_security' ); ?>

The problem is that I realized that there were various functions in javascript who wore the same name to have them executed.

What I thought was to also make it dynamic. However I’m not able to do the functions in JS work.

The original function in JS is the following:

register_ajax: function() {
            $('#thim-popup-login form[name=registerformpopup]').on('submit', function(e) {
                e.preventDefault();

                if (!thim_eduma.validate_form($(this))) {
                    return false;
                }

Tentei de várias formas mas não consegui. Tentei colocar um código php dentro do JS mas não deu. Também tentei o seguinte:

    register_ajax: function() {
        $('#thim-popup-login form[name="registerformpopup' + document.title + '"]').on('submit', function(e) {
                e.preventDefault();

                if (!thim_eduma.validate_form($(this))) {
                    return false;
                }

Can someone help me?

  • You have a syntax error in $('#thim-popup-login form['name=registerformpopup ' + document.title]')... this concatenation could be $('#thim-popup-login form[name="registerformpopup' + document.title + '"]')

  • Hi Sergio, all right? In fact the concatenation was wrong. Thank you! But it still didn’t work. :(

  • Dude, you don’t need to take the name of the form to use it. There are mts ways to catch it without necessarily using the name.

  • For example: $("form").on("submit", function(){ $(this) });...the $(this) is the form that has been submitted, no matter what is in it.

  • In the same php code there is the login and registration form. I believe this is the reason to use the name.

1 answer

0


You could use the attributeStartsWith jQuery. Follow the example of the site:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>attributeStartsWith demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<input name="newsletter">
<input name="milkman">
<input name="newsboy">

<script>
$( "input[name^='news']" ).val( "news here!" );
</script>

</body>
</html>

The result of this HML is 1° and 3° input with the text "news here!" because only they start with the word "news"

  • Incredibly worked, Murilo. I wrote like this in the JS code: $('#thim-popup-login form[name^="registerformpopup"]') Thank you so much for sharing your knowledge.

  • I’m glad it worked out! I’m glad I could help you!

Browser other questions tagged

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