Home > Net >  Using JavaScript to add an attribute to all <input type="file">
Using JavaScript to add an attribute to all <input type="file">

Time:12-16

My question is regarding a WordPress site. Using JavaScript or jQuery, on the entire front-end of the website, I would like to add the attribute accept="image/*" to all of the <input type="file">. Could someone please help me achieve this?

CodePudding user response:

Quite trivial:

document.querySelectorAll("[type=file]")
 .forEach(file => file.setAttribute("accept","image/*"))

You will still need to check the type of file on the server too

CodePudding user response:

Using jquery in wordpress

The following code uses jquery approach to solve your problem:

jQuery(document).ready($ => {

  $('input[type=file]').each(() => {
    $(this).attr('accept', 'image/*');
  });

})
  • Related