JS/JQuery: Dynamically add "pattern" and "title" attribute to form input

Keven

I have multiple form input elements. I would like to add a pattern attribute and a title attribute to each one, but rather than do so manually, I want to dynamically add the same pattern and title to each form input using JavaScript/jQuery.

This is what the form input fields look like now:

<input type="text" class="form-control" id="paycheck" />
<input type="text" class="form-control" id="investments" />
<input type="text" class="form-control" id="otherIncome" />

As an end result I would like each form input to look like the following:

<input pattern="\d*\.?\d*" title="blah blah blah" type="text" class="form-control" id="paycheck" />
etc...

As examples, I've tried the following for the pattern attribute, but none of them work:

$( "input" ).attr("pattern", '\d*\.?\d*');
$( "input" ).attr("pattern",  \d*\.?\d* );
$( ".formClass input" ).attr("pattern", '\d*\.?\d*');

$( "input" ).prop("pattern", '\d*\.?\d*');
$( "input" ).prop("pattern",  \d*\.?\d* );
$( ".formClass input" ).prop("pattern", '\d*\.?\d*');

...imagine something similar for the title attribute...
Keven

In the end, I found that the syntax was correct. There was an error somewhere else in the code preventing that statement from being run. Just goes to show that you should always make sure everything is good elsewhere in your code first.

However, I did learn a few things from this which I will note for others:

  1. The jQuery .attr() function will dynamically add any attribute you specify, so you don't need to put pattern="" in your form elements first in order for the value to be added or changed.
  2. Of important note, if you are going to dynamically add a regex using jQuery, YOU NEED TO ESCAPE certain characters.

The regex was originally \d*\.?\d* but in the DOM it was showing up as d*.?d* so when sending the regex through jQuery I escaped the backslashes like so: \\d*\\.?\\d*.

  1. Finally, I did not have to make my fields required for the regex to work. The HMTL5 validation only threw an error for me if I included incorrect text in the field, and when it threw an error, the form was not submitted. If I left the fields empty or put correct text in the fields, then no error was thrown. I'm up for an explanation if I'm wrong.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JS/JQuery: Dynamically add "pattern" and "title" attribute to form input

From Dev

Dynamically add title attribute to URL in HTML

From Dev

Dynamically add <form:input> textbox in jsp

From Dev

React: dynamically add input fields to form

From Dev

html5 input pattern attribute not working outside a form?

From Dev

How to dynamically add form input to reactive form angular

From Dev

Can I add pattern attribute with input type as number?

From Dev

Jquery Validation dynamically add rules for each input once form generated

From Dev

angular2 using input to dynamically add to form model

From Dev

Dynamically Add Customized HTML Form File Input with jQuery

From Dev

Jquery Validation dynamically add rules for each input once form generated

From Dev

Dynamically add user input with simple_form in rails

From Dev

Add / Remove input fields Dynamically to form (Symfony 3 Events / Ajax)

From Dev

Simple Form custom component does not add attribute to input

From Dev

Setting title attribute for a disabled input

From Dev

How to add data-title attribute to dynamically added row in jquery datatable?

From Dev

Add title dynamically in Orchard CMS

From Dev

How do I dynamically add a ng-pattern attribute to an html element?

From Dev

How do I dynamically add a ng-pattern attribute to an html element?

From Dev

Adding title attribute to select option dynamically with .append()

From Dev

How to set title attribute for HTML element dynamically?

From Dev

HTML Form Input Title Styling

From Dev

Change the value of input attribute dynamically

From Dev

How to add custom attribute to all simple_form input based on model name and attribute value?

From Dev

SAPUI5 Form Add Label and Input dynamically and set ID for Input but not function

From Dev

How to add dynamically attribute in VueJs

From Dev

dynamically add attribute to child element

From Dev

I am trying to add user input from the second form with title, description to my Data array but can't

From Dev

Adding form input fields dynamically

Related Related

  1. 1

    JS/JQuery: Dynamically add "pattern" and "title" attribute to form input

  2. 2

    Dynamically add title attribute to URL in HTML

  3. 3

    Dynamically add <form:input> textbox in jsp

  4. 4

    React: dynamically add input fields to form

  5. 5

    html5 input pattern attribute not working outside a form?

  6. 6

    How to dynamically add form input to reactive form angular

  7. 7

    Can I add pattern attribute with input type as number?

  8. 8

    Jquery Validation dynamically add rules for each input once form generated

  9. 9

    angular2 using input to dynamically add to form model

  10. 10

    Dynamically Add Customized HTML Form File Input with jQuery

  11. 11

    Jquery Validation dynamically add rules for each input once form generated

  12. 12

    Dynamically add user input with simple_form in rails

  13. 13

    Add / Remove input fields Dynamically to form (Symfony 3 Events / Ajax)

  14. 14

    Simple Form custom component does not add attribute to input

  15. 15

    Setting title attribute for a disabled input

  16. 16

    How to add data-title attribute to dynamically added row in jquery datatable?

  17. 17

    Add title dynamically in Orchard CMS

  18. 18

    How do I dynamically add a ng-pattern attribute to an html element?

  19. 19

    How do I dynamically add a ng-pattern attribute to an html element?

  20. 20

    Adding title attribute to select option dynamically with .append()

  21. 21

    How to set title attribute for HTML element dynamically?

  22. 22

    HTML Form Input Title Styling

  23. 23

    Change the value of input attribute dynamically

  24. 24

    How to add custom attribute to all simple_form input based on model name and attribute value?

  25. 25

    SAPUI5 Form Add Label and Input dynamically and set ID for Input but not function

  26. 26

    How to add dynamically attribute in VueJs

  27. 27

    dynamically add attribute to child element

  28. 28

    I am trying to add user input from the second form with title, description to my Data array but can't

  29. 29

    Adding form input fields dynamically

HotTag

Archive