-2
<form name="myform">
          <fieldset>
          <legend>Delivery Information</legend>

          <p>Country: <input pattern="^[A-Za-z]" type="text" name="textInput" size="25" placeholder="input country..."></p>
          <p>Street: <input pattern="^[A-Za-z0-9]" type="text" name="street" size="30" placeholder="input street..."></p>
          <p>City: <input pattern="^[A-Za-z ]" type="text" name="textInput" size="20" placeholder="input city..."></p>
          <p>Zip: <input pattern="(\d{5}([\-]\d{4})?)" type="text" name="zip" size="5" placeholder="#####"></p>
      </fieldset>
</form>

So, I have a form and I want to get all input elements by name "textInput"(I have other inputs so it ha) using Javascrip DOM, and add an attribute "oninvalid="please, only use letters".

Only Javascript, so no JQuery.

Toni Golac
  • 45
  • 4

2 Answers2

1

You can use this function:

function changeAttributes() {
    var x = document.getElementsByName("textInput");
    for(var i = 0; i < x.length, i++) {
        x[i].setAttribute("oninvalid", "please, only use letters");
    }
}
samuelnj
  • 1,627
  • 1
  • 10
  • 19
  • Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted – samuelnj Nov 07 '17 at 16:35
0

Try below:

 $('input[name=textInput]').attr("oninvalid", "please, only use letters");
Sociopath
  • 13,068
  • 19
  • 47
  • 75
swathi
  • 97
  • 1
  • 2