1

I have many input fields with type="text"

<input type="text" id="...">
<input type="text" id="...">
<input type="text" id="...">
...
<input type="text" id="...">
<input type="text" id="...">
<input type="text" id="...">

I want to prevent from typing type="text" on every input. Is there any way to group inputs like this:

<inputgroup type="text">
   <input id="...">
   <input id="...">
   <input id="...">
</inputgroup>

The solution can be coded with HTML, Javascript or anything else for WEB.

2
  • do you want to remove all type attribute? Commented Jan 31, 2017 at 18:48
  • No need for script, simply omit the type ... as Quentin already answered Commented Jan 31, 2017 at 19:08

4 Answers 4

2

No.

There is a way to group inputs: <fieldset> and <legend> but that is for grouping them based on what is going to be entered into them (such as for when you have multiple fields making up the different parts of an address).

There is no way, in HTML, to set the type attribute other than explicitly.

That said, type="text" is the default, so you can simply omit the attribute entirely (unless you depend on it for other reasons, such as with a CSS attribute selector).


You could also generate the HTML programmatically (e.g. with a template language).

Sign up to request clarification or add additional context in comments.

Comments

1

You can create a javascript function to dynamically inject input tags inside a div

(function(){
var inject='';
for(var i=0;i<10;i++){
    inject+="<input type='text' value='"+i+"' id='id_"+i+"'/><br/>"
}
$('#inject').html(inject);

})();


<div id="inject">
//input tags will be injected here after function execution
</div>

Demo : https://jsfiddle.net/abk0hvts/

1 Comment

If this solved your problem then choose it as Correct Answer to help others in future.
0

You have to use JavaScript/jQuery to achieve what you are looking for. Here is the DEMO for what you are looking for.

    <!--Group all your input fields within a 'div' and give this div a unique ID-->
    <div id="inputGroupText">
      <input id='a' />
      <input id='b' />
      <input id='c' />
    </div>

<script>
     $(document).ready(function(){
          //iterate through the div which groups all the 'input' fields
          $('#inpuGroupText').children('input').each(function () {
               // you can substitute 'text' with 'email', 'password' etc. depending on your need
               $(this).attr('type', 'text');
          });
     });
</script>

Note: If you just want to avoid writing type='text' then you can just ignore it because 'text' is the default value for 'type' attribute so if the browser will automatically consider the input field as type=text if you have not specified value for it.

2 Comments

Thank you, I will use your solution! @Saumil Soni
@BrenGinthom Glad could help.
-1

Same solution but the code is much more readable :)

<script>
    $(document).ready(function () {
      var parent = document.getElementById('autobot');
      var tags = parent.getElementsByTagName('input');
      for (var i = 0; i < tags.length; i++) {
        tags[i].setAttribute('type', 'text');
      }
    });
</script>

<div id="autobot">        
    <input>
    <input>
    <input>
  </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.