简体   繁体   中英

How to set input value by JavaScript document.querySelector

Just I want to add input value using javascript. Here's priject link !> add to cart > Proceed to Checkout || then you get the checkout page.

Actually I want to add automatically Zipcode value in the checkout page. I try this methood but dosen't work

document.querySelector('#shipping-new-address-form div:nth-child(8) input').value = '12345';

Can anyone help to solve this issue?

You can use chrome console to ensure about your query finding target element and setting its value, exactly because your selector may not be right.

It should be like that

document.querySelector('input[name = "' + prop + '"]').setAttribute('placeholder', data[prop])

There is another answer like this here QuerySelector set text value

on the checkout page, you can define onload event.

<body onload="fillZipCode()">
<script>
function fillZipCode() {
  document.querySelector('#shipping-new-address-form div:nth-child(8) input').value = '12345';
</script>

If you want to add input field and its value using JavaScript then you can use below script.

<script>

function myFunction() 
{
  var input = document.createElement("INPUT");
  input.setAttribute("type", "text");
  input.setAttribute("value", "1234");
  document.body.appendChild(input);
}

myFunction();
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM