0

I created a form using Profile Builder.

Here is link to Form: Form Link

I installed WordPress plugin "Insert Header and Footer" and I tried to run jQuery script with just an alert and it was working.

But, it is not running with below code. The code shows in inspect element but it has no effect.

    <script>
$( "#username" ).attr("pattern", '^[0-9]{8}[A-Z]$').prop('required', true)
    </script>

I want to take selected number in form field. It's a simple script, why is the plugin not running it?

1 Answer 1

3

$ is not available to your script. You can use a plain javascript option like below:

<script>
  // create a ref to your dom element
  var username = document.querySelector("#username")
  
  // set pattern attribute
  username.setAttribute("pattern", '^[0-9]{8}[A-Z]$');

  // set as required
  username.required = true;
</script>

I tried form the console and this version works: enter image description here enter image description here

Alternatively you can swap the $ for jQuery like below:

<script>
  jQuery( "#username" ).attr("pattern", '^[0-9]{8}[A-Z]$').prop('required', true);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Please try the plain js version. I just ran it in the console and it worked. You also have a couple console errors that could be conflicting.
how to check which version of JavaScript is working on that element through console?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.