javascriptdom-manipulationgetelementsbyname

form value fill using javascript


i am filling this value using javascript code. after that i am able to see this value in cosole. but value not visible in box. due to this i am unable to click submit button.this element is in a form tag.images link https://i.sstatic.net/quZ8f.png https://i.sstatic.net/r9KgX.jpg

function myfn(){
  document.getElementsByName("ELEC_CURR_B_PHASE").value="415V";
  var x= document.getElementsByName("ELEC_CURR_B_PHASE").value="415V";
  console.log(x);

};myfn()

result after running code
inspect element image


Solution

  • getElementsByName() returns a NodeList and to set the property value, select the first index.

    function myfn(){
      document.getElementsByName("ELEC_CURR_B_PHASE").value="415V";
      var x= document.getElementsByName("ELEC_CURR_B_PHASE")[0].value="415V";
      console.log(x);
    };
    myfn();