4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

いちいち見出しに「必須入力」と書く労力

4
Posted at

自動的に「必須入力」を付記させる

form.html
<label>
	<span>メールアドレス(※必須)</span>
	<input type=>
</label>

…なんてことをしますが、書き忘れたり、仕様変更で必須ではなくなったりしますね。表記と挙動が違うのは避けたいものです。

inputタグに"required"が付いているものが必須なわけですから、自動的に「必須」と付記させましょう。下記のファイルを置いたら、各htmlページに

```の1行を追加してください。

```javascript:check_required.js
document.addEventListener('DOMContentLoaded',function(event){
	check_required();
},false);

function check_required()
{
	for(i=0;i<document.forms.length;i++){
		for(j=0;j<document.forms[i].elements.length;j++){
			e=document.forms[i].elements[j];
			if(e.attributes.getNamedItem("required")){
				e.placeholder+="[必須]";
				e.previousElementSibling.innerHTML+="[必須]";
			}
		}
	}
}

これで少しは手間が省けますね。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?