jQuery code to detect whether form fields have the default value when a checkbox is unchecked












1














How could I rewrite this jquery code to compact it in a more elegant form:






$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('');
}
else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('users');
}

if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('');
}
else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('user_id');
}

if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('');
}
else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('user_name');
}

if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('');
}
else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('user_email');
}

if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('');
}
else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('user_pass');
}

if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('');
}
else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('joining_date');
}
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">

<h2>DB Table</h2>

<p>
Activate the slider if you already have your own database table created!
</p>

<div class="inputCheckBox">
<input type="checkbox" name="table_exist" />
</div>

<p>
Edit here only if you have your own predifined table header names!
</p>

<div class="inputBox">
<input type="text" name="table_name" value="users" required />
<label>Table Name</label>
</div>

<div class="inputBox">
<input type="text" name="user_id" value="user_id" required />
<label>User ID</label>
</div>

<div class="inputBox">
<input type="text" name="user_name" value="user_name" required />
<label>User Name</label>
</div>

<div class="inputBox">
<input type="text" name="user_email" value="user_email" required />
<label>User E-mail</label>
</div>

<div class="inputBox">
<input type="text" name="user_pass" value="user_pass" required />
<label>User Password</label>
</div>

<div class="inputBox">
<input type="text" name="joining_date" value="joining_date" required />
<label>User Added Date</label>
</div>

<button type="submit" name="btn-install">
Install
</button>

</form>





The whole process is to identify if the fields have the default values when a checkbox is unchecked. If the state is changed to checked then empty all fields to input custom information/values. If unchecked again it returns the initial values.










share|improve this question





























    1














    How could I rewrite this jquery code to compact it in a more elegant form:






    $(document).ready(function(){
    $('input[name="table_exist"]').click(function(){
    if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="table_name"]').val('');
    }
    else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="table_name"]').val('users');
    }

    if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="user_id"]').val('');
    }
    else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="user_id"]').val('user_id');
    }

    if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="user_name"]').val('');
    }
    else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="user_name"]').val('user_name');
    }

    if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="user_email"]').val('');
    }
    else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="user_email"]').val('user_email');
    }

    if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="user_pass"]').val('');
    }
    else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="user_pass"]').val('user_pass');
    }

    if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="joining_date"]').val('');
    }
    else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
    {
    $('input[name="joining_date"]').val('joining_date');
    }
    });
    });

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form method="POST">

    <h2>DB Table</h2>

    <p>
    Activate the slider if you already have your own database table created!
    </p>

    <div class="inputCheckBox">
    <input type="checkbox" name="table_exist" />
    </div>

    <p>
    Edit here only if you have your own predifined table header names!
    </p>

    <div class="inputBox">
    <input type="text" name="table_name" value="users" required />
    <label>Table Name</label>
    </div>

    <div class="inputBox">
    <input type="text" name="user_id" value="user_id" required />
    <label>User ID</label>
    </div>

    <div class="inputBox">
    <input type="text" name="user_name" value="user_name" required />
    <label>User Name</label>
    </div>

    <div class="inputBox">
    <input type="text" name="user_email" value="user_email" required />
    <label>User E-mail</label>
    </div>

    <div class="inputBox">
    <input type="text" name="user_pass" value="user_pass" required />
    <label>User Password</label>
    </div>

    <div class="inputBox">
    <input type="text" name="joining_date" value="joining_date" required />
    <label>User Added Date</label>
    </div>

    <button type="submit" name="btn-install">
    Install
    </button>

    </form>





    The whole process is to identify if the fields have the default values when a checkbox is unchecked. If the state is changed to checked then empty all fields to input custom information/values. If unchecked again it returns the initial values.










    share|improve this question



























      1












      1








      1







      How could I rewrite this jquery code to compact it in a more elegant form:






      $(document).ready(function(){
      $('input[name="table_exist"]').click(function(){
      if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="table_name"]').val('');
      }
      else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="table_name"]').val('users');
      }

      if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_id"]').val('');
      }
      else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_id"]').val('user_id');
      }

      if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_name"]').val('');
      }
      else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_name"]').val('user_name');
      }

      if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_email"]').val('');
      }
      else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_email"]').val('user_email');
      }

      if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_pass"]').val('');
      }
      else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_pass"]').val('user_pass');
      }

      if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="joining_date"]').val('');
      }
      else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="joining_date"]').val('joining_date');
      }
      });
      });

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <form method="POST">

      <h2>DB Table</h2>

      <p>
      Activate the slider if you already have your own database table created!
      </p>

      <div class="inputCheckBox">
      <input type="checkbox" name="table_exist" />
      </div>

      <p>
      Edit here only if you have your own predifined table header names!
      </p>

      <div class="inputBox">
      <input type="text" name="table_name" value="users" required />
      <label>Table Name</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_id" value="user_id" required />
      <label>User ID</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_name" value="user_name" required />
      <label>User Name</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_email" value="user_email" required />
      <label>User E-mail</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_pass" value="user_pass" required />
      <label>User Password</label>
      </div>

      <div class="inputBox">
      <input type="text" name="joining_date" value="joining_date" required />
      <label>User Added Date</label>
      </div>

      <button type="submit" name="btn-install">
      Install
      </button>

      </form>





      The whole process is to identify if the fields have the default values when a checkbox is unchecked. If the state is changed to checked then empty all fields to input custom information/values. If unchecked again it returns the initial values.










      share|improve this question















      How could I rewrite this jquery code to compact it in a more elegant form:






      $(document).ready(function(){
      $('input[name="table_exist"]').click(function(){
      if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="table_name"]').val('');
      }
      else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="table_name"]').val('users');
      }

      if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_id"]').val('');
      }
      else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_id"]').val('user_id');
      }

      if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_name"]').val('');
      }
      else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_name"]').val('user_name');
      }

      if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_email"]').val('');
      }
      else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_email"]').val('user_email');
      }

      if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_pass"]').val('');
      }
      else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_pass"]').val('user_pass');
      }

      if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="joining_date"]').val('');
      }
      else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="joining_date"]').val('joining_date');
      }
      });
      });

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <form method="POST">

      <h2>DB Table</h2>

      <p>
      Activate the slider if you already have your own database table created!
      </p>

      <div class="inputCheckBox">
      <input type="checkbox" name="table_exist" />
      </div>

      <p>
      Edit here only if you have your own predifined table header names!
      </p>

      <div class="inputBox">
      <input type="text" name="table_name" value="users" required />
      <label>Table Name</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_id" value="user_id" required />
      <label>User ID</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_name" value="user_name" required />
      <label>User Name</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_email" value="user_email" required />
      <label>User E-mail</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_pass" value="user_pass" required />
      <label>User Password</label>
      </div>

      <div class="inputBox">
      <input type="text" name="joining_date" value="joining_date" required />
      <label>User Added Date</label>
      </div>

      <button type="submit" name="btn-install">
      Install
      </button>

      </form>





      The whole process is to identify if the fields have the default values when a checkbox is unchecked. If the state is changed to checked then empty all fields to input custom information/values. If unchecked again it returns the initial values.






      $(document).ready(function(){
      $('input[name="table_exist"]').click(function(){
      if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="table_name"]').val('');
      }
      else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="table_name"]').val('users');
      }

      if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_id"]').val('');
      }
      else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_id"]').val('user_id');
      }

      if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_name"]').val('');
      }
      else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_name"]').val('user_name');
      }

      if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_email"]').val('');
      }
      else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_email"]').val('user_email');
      }

      if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_pass"]').val('');
      }
      else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_pass"]').val('user_pass');
      }

      if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="joining_date"]').val('');
      }
      else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="joining_date"]').val('joining_date');
      }
      });
      });

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <form method="POST">

      <h2>DB Table</h2>

      <p>
      Activate the slider if you already have your own database table created!
      </p>

      <div class="inputCheckBox">
      <input type="checkbox" name="table_exist" />
      </div>

      <p>
      Edit here only if you have your own predifined table header names!
      </p>

      <div class="inputBox">
      <input type="text" name="table_name" value="users" required />
      <label>Table Name</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_id" value="user_id" required />
      <label>User ID</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_name" value="user_name" required />
      <label>User Name</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_email" value="user_email" required />
      <label>User E-mail</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_pass" value="user_pass" required />
      <label>User Password</label>
      </div>

      <div class="inputBox">
      <input type="text" name="joining_date" value="joining_date" required />
      <label>User Added Date</label>
      </div>

      <button type="submit" name="btn-install">
      Install
      </button>

      </form>





      $(document).ready(function(){
      $('input[name="table_exist"]').click(function(){
      if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="table_name"]').val('');
      }
      else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="table_name"]').val('users');
      }

      if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_id"]').val('');
      }
      else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_id"]').val('user_id');
      }

      if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_name"]').val('');
      }
      else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_name"]').val('user_name');
      }

      if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_email"]').val('');
      }
      else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_email"]').val('user_email');
      }

      if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_pass"]').val('');
      }
      else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="user_pass"]').val('user_pass');
      }

      if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="joining_date"]').val('');
      }
      else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
      {
      $('input[name="joining_date"]').val('joining_date');
      }
      });
      });

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <form method="POST">

      <h2>DB Table</h2>

      <p>
      Activate the slider if you already have your own database table created!
      </p>

      <div class="inputCheckBox">
      <input type="checkbox" name="table_exist" />
      </div>

      <p>
      Edit here only if you have your own predifined table header names!
      </p>

      <div class="inputBox">
      <input type="text" name="table_name" value="users" required />
      <label>Table Name</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_id" value="user_id" required />
      <label>User ID</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_name" value="user_name" required />
      <label>User Name</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_email" value="user_email" required />
      <label>User E-mail</label>
      </div>

      <div class="inputBox">
      <input type="text" name="user_pass" value="user_pass" required />
      <label>User Password</label>
      </div>

      <div class="inputBox">
      <input type="text" name="joining_date" value="joining_date" required />
      <label>User Added Date</label>
      </div>

      <button type="submit" name="btn-install">
      Install
      </button>

      </form>






      javascript jquery form






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 6 hours ago

























      asked 10 hours ago









      user1286956

      154




      154






















          2 Answers
          2






          active

          oldest

          votes


















          2














          There're multiple ways to do that. You should use a loop.



          I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.






          $(document).ready(function(){
          $('input[name="table_exist"]').click(function(){
          var table_exist_checked = $('input[name="table_exist"]').is(':checked');

          var fields = [
          'table_name',
          'user_id',
          'user_name',
          'user_email',
          'user_pass',
          'joining_date',
          ]; // Your field names

          // iterate over all fieldnames
          for(var index of fields) {
          var fieldName = fields[index]; // extract current fieldName
          var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat

          // This is ur if-else. I do not exactly know if this is correct
          if($elem.val() == fieldName && table_exist_checked){
          $elem.val('');
          } else if($elem.val()=='' && !table_exist_checked){
          $elem.val(fieldName);
          }
          }

          }); });








          share|improve this answer








          New contributor




          ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


























            0














            Following @ArayniMax example I fixed it this way:



            $(document).ready(function(){
            $('input[name="table_exist"]').click(function(){

            var inputCheck = $('input[name="table_exist"]').is(':checked');

            var fields = {
            'table_name': 'users',
            'user_id': 'user_id',
            'user_name': 'user_name',
            'user_email': 'user_email',
            'user_pass': 'user_pass',
            'joining_date': 'joining_date'
            };

            $.each(fields, function(key, value) {

            var fieldInput = $('input[name="' + key + '"]');

            if(fieldInput.val()==value && inputCheck)
            {
            fieldInput.val('');
            }
            else if(fieldInput.val()=='' && !inputCheck)
            {
            fieldInput.val(value);
            }
            });
            });
            });


            Thank you for your help everyone!






            share|improve this answer























              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              });
              });
              }, "mathjax-editing");

              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "196"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210361%2fjquery-code-to-detect-whether-form-fields-have-the-default-value-when-a-checkbox%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              There're multiple ways to do that. You should use a loop.



              I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.






              $(document).ready(function(){
              $('input[name="table_exist"]').click(function(){
              var table_exist_checked = $('input[name="table_exist"]').is(':checked');

              var fields = [
              'table_name',
              'user_id',
              'user_name',
              'user_email',
              'user_pass',
              'joining_date',
              ]; // Your field names

              // iterate over all fieldnames
              for(var index of fields) {
              var fieldName = fields[index]; // extract current fieldName
              var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat

              // This is ur if-else. I do not exactly know if this is correct
              if($elem.val() == fieldName && table_exist_checked){
              $elem.val('');
              } else if($elem.val()=='' && !table_exist_checked){
              $elem.val(fieldName);
              }
              }

              }); });








              share|improve this answer








              New contributor




              ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                2














                There're multiple ways to do that. You should use a loop.



                I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.






                $(document).ready(function(){
                $('input[name="table_exist"]').click(function(){
                var table_exist_checked = $('input[name="table_exist"]').is(':checked');

                var fields = [
                'table_name',
                'user_id',
                'user_name',
                'user_email',
                'user_pass',
                'joining_date',
                ]; // Your field names

                // iterate over all fieldnames
                for(var index of fields) {
                var fieldName = fields[index]; // extract current fieldName
                var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat

                // This is ur if-else. I do not exactly know if this is correct
                if($elem.val() == fieldName && table_exist_checked){
                $elem.val('');
                } else if($elem.val()=='' && !table_exist_checked){
                $elem.val(fieldName);
                }
                }

                }); });








                share|improve this answer








                New contributor




                ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





















                  2












                  2








                  2






                  There're multiple ways to do that. You should use a loop.



                  I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.






                  $(document).ready(function(){
                  $('input[name="table_exist"]').click(function(){
                  var table_exist_checked = $('input[name="table_exist"]').is(':checked');

                  var fields = [
                  'table_name',
                  'user_id',
                  'user_name',
                  'user_email',
                  'user_pass',
                  'joining_date',
                  ]; // Your field names

                  // iterate over all fieldnames
                  for(var index of fields) {
                  var fieldName = fields[index]; // extract current fieldName
                  var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat

                  // This is ur if-else. I do not exactly know if this is correct
                  if($elem.val() == fieldName && table_exist_checked){
                  $elem.val('');
                  } else if($elem.val()=='' && !table_exist_checked){
                  $elem.val(fieldName);
                  }
                  }

                  }); });








                  share|improve this answer








                  New contributor




                  ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  There're multiple ways to do that. You should use a loop.



                  I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.






                  $(document).ready(function(){
                  $('input[name="table_exist"]').click(function(){
                  var table_exist_checked = $('input[name="table_exist"]').is(':checked');

                  var fields = [
                  'table_name',
                  'user_id',
                  'user_name',
                  'user_email',
                  'user_pass',
                  'joining_date',
                  ]; // Your field names

                  // iterate over all fieldnames
                  for(var index of fields) {
                  var fieldName = fields[index]; // extract current fieldName
                  var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat

                  // This is ur if-else. I do not exactly know if this is correct
                  if($elem.val() == fieldName && table_exist_checked){
                  $elem.val('');
                  } else if($elem.val()=='' && !table_exist_checked){
                  $elem.val(fieldName);
                  }
                  }

                  }); });








                  $(document).ready(function(){
                  $('input[name="table_exist"]').click(function(){
                  var table_exist_checked = $('input[name="table_exist"]').is(':checked');

                  var fields = [
                  'table_name',
                  'user_id',
                  'user_name',
                  'user_email',
                  'user_pass',
                  'joining_date',
                  ]; // Your field names

                  // iterate over all fieldnames
                  for(var index of fields) {
                  var fieldName = fields[index]; // extract current fieldName
                  var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat

                  // This is ur if-else. I do not exactly know if this is correct
                  if($elem.val() == fieldName && table_exist_checked){
                  $elem.val('');
                  } else if($elem.val()=='' && !table_exist_checked){
                  $elem.val(fieldName);
                  }
                  }

                  }); });





                  $(document).ready(function(){
                  $('input[name="table_exist"]').click(function(){
                  var table_exist_checked = $('input[name="table_exist"]').is(':checked');

                  var fields = [
                  'table_name',
                  'user_id',
                  'user_name',
                  'user_email',
                  'user_pass',
                  'joining_date',
                  ]; // Your field names

                  // iterate over all fieldnames
                  for(var index of fields) {
                  var fieldName = fields[index]; // extract current fieldName
                  var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat

                  // This is ur if-else. I do not exactly know if this is correct
                  if($elem.val() == fieldName && table_exist_checked){
                  $elem.val('');
                  } else if($elem.val()=='' && !table_exist_checked){
                  $elem.val(fieldName);
                  }
                  }

                  }); });






                  share|improve this answer








                  New contributor




                  ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 7 hours ago









                  ArayniMax

                  362




                  362




                  New contributor




                  ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  ArayniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.

























                      0














                      Following @ArayniMax example I fixed it this way:



                      $(document).ready(function(){
                      $('input[name="table_exist"]').click(function(){

                      var inputCheck = $('input[name="table_exist"]').is(':checked');

                      var fields = {
                      'table_name': 'users',
                      'user_id': 'user_id',
                      'user_name': 'user_name',
                      'user_email': 'user_email',
                      'user_pass': 'user_pass',
                      'joining_date': 'joining_date'
                      };

                      $.each(fields, function(key, value) {

                      var fieldInput = $('input[name="' + key + '"]');

                      if(fieldInput.val()==value && inputCheck)
                      {
                      fieldInput.val('');
                      }
                      else if(fieldInput.val()=='' && !inputCheck)
                      {
                      fieldInput.val(value);
                      }
                      });
                      });
                      });


                      Thank you for your help everyone!






                      share|improve this answer




























                        0














                        Following @ArayniMax example I fixed it this way:



                        $(document).ready(function(){
                        $('input[name="table_exist"]').click(function(){

                        var inputCheck = $('input[name="table_exist"]').is(':checked');

                        var fields = {
                        'table_name': 'users',
                        'user_id': 'user_id',
                        'user_name': 'user_name',
                        'user_email': 'user_email',
                        'user_pass': 'user_pass',
                        'joining_date': 'joining_date'
                        };

                        $.each(fields, function(key, value) {

                        var fieldInput = $('input[name="' + key + '"]');

                        if(fieldInput.val()==value && inputCheck)
                        {
                        fieldInput.val('');
                        }
                        else if(fieldInput.val()=='' && !inputCheck)
                        {
                        fieldInput.val(value);
                        }
                        });
                        });
                        });


                        Thank you for your help everyone!






                        share|improve this answer


























                          0












                          0








                          0






                          Following @ArayniMax example I fixed it this way:



                          $(document).ready(function(){
                          $('input[name="table_exist"]').click(function(){

                          var inputCheck = $('input[name="table_exist"]').is(':checked');

                          var fields = {
                          'table_name': 'users',
                          'user_id': 'user_id',
                          'user_name': 'user_name',
                          'user_email': 'user_email',
                          'user_pass': 'user_pass',
                          'joining_date': 'joining_date'
                          };

                          $.each(fields, function(key, value) {

                          var fieldInput = $('input[name="' + key + '"]');

                          if(fieldInput.val()==value && inputCheck)
                          {
                          fieldInput.val('');
                          }
                          else if(fieldInput.val()=='' && !inputCheck)
                          {
                          fieldInput.val(value);
                          }
                          });
                          });
                          });


                          Thank you for your help everyone!






                          share|improve this answer














                          Following @ArayniMax example I fixed it this way:



                          $(document).ready(function(){
                          $('input[name="table_exist"]').click(function(){

                          var inputCheck = $('input[name="table_exist"]').is(':checked');

                          var fields = {
                          'table_name': 'users',
                          'user_id': 'user_id',
                          'user_name': 'user_name',
                          'user_email': 'user_email',
                          'user_pass': 'user_pass',
                          'joining_date': 'joining_date'
                          };

                          $.each(fields, function(key, value) {

                          var fieldInput = $('input[name="' + key + '"]');

                          if(fieldInput.val()==value && inputCheck)
                          {
                          fieldInput.val('');
                          }
                          else if(fieldInput.val()=='' && !inputCheck)
                          {
                          fieldInput.val(value);
                          }
                          });
                          });
                          });


                          Thank you for your help everyone!







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 5 hours ago

























                          answered 6 hours ago









                          user1286956

                          154




                          154






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Code Review Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210361%2fjquery-code-to-detect-whether-form-fields-have-the-default-value-when-a-checkbox%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              What visual should I use to simply compare current year value vs last year in Power BI desktop

                              How to ignore python UserWarning in pytest?

                              Alexandru Averescu