0

Are there templating systems that permit defining default values for attributes of HTML elements? Any page that contains the given HTML element and that is generated using the given template will have HTML generated with the value of the referred attribute of the referred element set to the defined default value.

<form method="post">

Say I wish to set the default value of the 'method' attribute of the 'form' element to 'post'. This should only be done if the page does not explicitly define the 'method' attribute.

Diptendu Dutta
  • 331
  • 1
  • 3
  • 12
  • The default for the `method` attribute of `
    ` is `GET` [as per the spec](http://stackoverflow.com/a/2314441/1331451). You would have to build something that creates HTML for you functionally and that you call with arguments to define defaults. You're not telling us what kind of technology you are working with. HTML alone does not have templating engines.
    – simbabque May 21 '17 at 10:41
  • I am exploring various templating engines to see if such support is provided. The reference to
    is just for an example, I plan to use the dafault value feature for various other cases such as 'size' attribute of 'input' elements.
    – Diptendu Dutta May 21 '17 at 10:57

1 Answers1

0

HTML do have some default values set for several attributes. Am not sure about the templating engines, but if you want to set custom defaults, you can use JavaScript.. say, for example, form element.

//get all form elements
var getElms = document.getElementsByTagName('form');

//loop over these elements, check if method attr exist, if not then set post as default
for(var i = 0, elms = getElms.length; i < elms; i++) {
    if(!getElms[i].hasAttribute('method')) {
    getElms[i].setAttribute("method", "post");
  }
}
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278