Can I mark a checkbox per button?

Asked

Viewed 362 times

-1

I need to trigger a <checkbox> by a button. It can be through Javascript or PHP, something that causes the unique value of <checkbox> be selected.

I want it because I intend to hide it and just trigger it for a <button>.

<td><?php echo utf8_encode($dados['ped_valid_proposta']);?>&nbsp;dias</td>
<td><button class="btn btn-primary" type="submit"><i class="fas fa-search"></i></button></td>
<td>
  <div class="form-check">
    <input type="checkbox" class="form-check-input" id="my_checkbox" name="os_codigo1" value="<?php echo utf8_encode($dados['ped_codigo'])?>">
    <label class="form-check-label" for="exampleCheck1">Selecionar</label>
  </div>
</td>

In that case <checkbox> is within a repeating structure that can select query by query, but I want that by clicking on the "magnifying glass" button I can trigger the <checkbox> correspondent.

  • If the checkbox is going to stay hidden, how will you know that it was triggered?

  • You want to be submitted only 1 checkbox or can be several that have been triggered?

  • Your question is a bit confusing. If it is a repetition structure there will be several checkboxes on the screen at the same time as it seems to me. Also, I noticed that you have in your code a Label, but the for="" of it is for an ID that does not exist, because the input:checkbox above has an ID with a name other than related to Label...

  • @hugocsl Really mt confused. But I believe he wants to remove the label, since he wants the checkbox to be triggered by the button

  • Sorry it took me so long to get the answer. I have already made a repeating structure that creates several checkboxes and when I select determining table field(This is all within a table) I send for $_SESSION[''] and the checkbox serves to differentiate the various Insert that the field 'code' has. I have a button with "magnifying glass" icon and it submits the checkbox, but just like it there are several of these buttons for each line of Insert. I would like the button to trigger the checkbox that would have <pre>display: None;</pre> or another way to hide it. Sorry for the confusion.

1 answer

0


If I understand the question correctly, you will have a table with several lines, and each row will have a checkbox, with the same name, which will be checked by clicking the button of the same line.

In this case you should use the appropriate type for this, ie the radio and not checkbox. A set of type="radio" with the same name only allows 1 of them to be checked.

In this case you will not need the label, since the radio will be checked via button. Another thing that becomes useless is the id="my_checkbox", that you can convert into class and hide the radio via CSS:

<input type="radio" class="form-check-input my_checkbox" name="os_codigo1" value="<?php echo utf8_encode($dados['ped_codigo'])?>">

CSS:

.my_checkbox{
   display: none;
}

It is also necessary to put the type of buttons as button and not as submit:

<button class="btn btn-primary" type="button"><i class="fas fa-search"></i></button>
                                        ↑

Via Javascript you can check the radio from the same line as the button with:

document.addEventListener("DOMContentLoaded", function(){
   // seleciona todos os botões
   var botoes = document.querySelectorAll("td button.btn[type=button]");
   // faz um laço percorrendo os botões adicionando um evento de click
   for(var x=0; x<botoes.length; x++){
      botoes[x].onclick = function(){
         // busca o respectivo radio na mesma linha e checa ele
         this.closest("tr").querySelector("[type=radio]").checked = true;
      }
   }
});

Example:

document.addEventListener("DOMContentLoaded", function(){
   var botoes = document.querySelectorAll("td button.btn[type=button]");
   for(var x=0; x<botoes.length; x++){
      botoes[x].onclick = function(){
         this.closest("tr").querySelector("[type=radio]").checked = true;
      }
   }
});
.my_checkbox{
   /*display: none;*/
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
Deixei os radios visíveis apenas como exemplo para visualização:
<table>
   <tr>
      <td>2&nbsp;dias</td>
      <td><button class="btn btn-primary" type="button"><i class="fas fa-search"></i></button></td>
      <td>
         <div class="form-check">
            <input type="radio" class="form-check-input my_checkbox" name="os_codigo1" value="2">
         </div>
      </td>
   </tr>
   <tr>
      <td>3&nbsp;dias</td>
      <td><button class="btn btn-primary" type="button"><i class="fas fa-search"></i></button></td>
      <td>
         <div class="form-check">
            <input type="radio" class="form-check-input my_checkbox" name="os_codigo1" value="3">
         </div>
      </td>
   </tr>
</table>

  • Thank you for your patience, many doubts have been resolved, but in the case of Label it does not have a function in logic and is more for the user. In the most, very grateful.

  • You’re welcome. As you’re new here, know how to thank at this link. Abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.