0

I want to make condition if database have data js will give attr required but if database not have a single data, attr required wil removed

this for my json

$(document).ready(function () {

// ================== CHANGE ====================

$("#no_reg").change(function(e) {
console.log(e);
var no_reg_id = e.target.value;
$.get("/PickStandart/" + no_reg_id, function(data) {
  console.log(data);

  $('#standart').empty();
  $('#standart').append('<option value="">-- Select One --</option>');
  $.each(data, function (index, row) {
      if (row.name != null) {
        $('#standart').attr('required', 'required');
      }
      else {
        $('#standart').removeAttr("required");
      }

      $('#standart').append('<option value="'+ row.id +'">' + row.name + '</option>')
    });

});
});

});

my problem is, I cant remove required attr if data is null

this for my json code

$bagian = Bagian::where('id', '=', $standart)->first();

if ($bagian->standart_klausul == 1) {
  $standart = Standart::where('aktif', '=', 1)->get();
  return response()->json($standart);
}
Ihsan
  • 35
  • 10
  • Did you check [this](https://stackoverflow.com/questions/19166685/jquery-add-required-to-input-fields) and [this](https://stackoverflow.com/questions/13951336/jquery-removing-html5-required-attribute)? – Imesha Sudasingha Jun 12 '18 at 04:56
  • yeah, Im not wrong in .attr or .removeAttr but I get error if database send data and if that data is null, I want to remove required but not removed @ImeshaSudasingha – Ihsan Jun 12 '18 at 05:08
  • In javascript we will get `undefined` if `data.name` is not available. So, use `if(data.name)` in the condition. If condition will evaluate `data.name` and consider as false if `data.name` is `null or undefined` – Imesha Sudasingha Jun 12 '18 at 05:11
  • can you fix my code and I will try it @ImeshaSudasingha – Ihsan Jun 12 '18 at 05:14

1 Answers1

0

Try this code:

$(document).ready(function () {

// ================== CHANGE ====================

$("#no_reg").change(function(e) {
console.log(e);
var no_reg_id = e.target.value;
$.get("/PickStandart/" + no_reg_id, function(data) {
  console.log(data);

  $('#standart').empty();
  $('#standart').append('<option value="">-- Select One --</option>');
  $.each(data, function (index, row) {
      if (row.name) {
        $('#standart').attr('required', 'required');
      }
      else {
        $('#standart').removeAttr("required");
      }

      $('#standart').append('<option value="'+ row.id +'">' + row.name + '</option>')
    });

});
});

});

Note the if condition. I have used row.name instead. As I mentioned in a comment,

In javascript we will get undefined if data.name is not available. So, use if(data.name) in the condition. If condition will evaluate data.name and consider as false if data.name is null or undefined

Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
  • its not working for me, remove required is not working – Ihsan Jun 12 '18 at 05:21
  • How does your `row` json look like? – Imesha Sudasingha Jun 12 '18 at 05:23
  • No, can you add the json response you are getting? put a `console.log(row)` and comment the output. – Imesha Sudasingha Jun 12 '18 at 05:26
  • this data I get `[{"id":1,"name":"OS 1","klausul":1,"aktif":1},{"id":2,"name":"OS 2","klausul":0,"aktif":1},{"id":3,"name":"OS 3","klausul":1,"aktif":1},{"id":4,"name":"OS 4","klausul":1,"aktif":1},{"id":5,"name":"OS 5","klausul":0,"aktif":1}]` – Ihsan Jun 12 '18 at 05:28
  • According to your data, all rows have a valid `name`. So, your `else` won't be called anytime. If this is not what you want to do, you have a problem in your logic. – Imesha Sudasingha Jun 12 '18 at 05:33
  • thats my problem and how I fix that, but if data is nol, `console.log(row)` dont have any value – Ihsan Jun 12 '18 at 05:38
  • Then add `if (row) { $('#standart').attr('required', 'required'); } else { $('#standart').removeAttr("required"); }` in before the `$.each()`. It will do. – Imesha Sudasingha Jun 12 '18 at 06:25