0

I am having some trouble doing an if statement since most of the tutorial videos and other forums, I saw people only using variable, example $value, but I want to use if statement on the input name itself. Is that possible, if so could you help me out? Thanks a lot. Sorry if you are feeling confused of what I want maybe, if you see the code, you might understand, I don't really quite know how to say it in words.

test.blade.php

 <div class="form-group">
        <label class="col-md-2"><b>Test:</b></label>
      <div class="col-md-6">
        <input type="radio" name="test" value="Yes"> Yes<br>
        <input type="radio" name="test" value="No"> No<br>
        <input type="radio" name="test" value="Pending"> Pending<br> 
      </div>

@if(<input type="radio" name = "test" value="Yes"> || <input type="radio" name = "test" value="Pending">)
//show some other kind of input types such as :
       <div class="form-group">
        <label class="col-md-2"><b>Training Schedule:</b></label>
      <div class="col-md-6">
        <input type="radio" name="training_schedule" value="Yes"> Yes<br>
        <input type="radio" name="training_schedule" value="No"> No<br>
      </div>
@else
//make the other inputs value become NIL instead of showing
Something like:
    name = "training_schedule" value = "NIL"
    @endif
4
  • You can't do like that. Just use javascipt to do that. Commented Nov 9, 2017 at 3:04
  • Do you the name of what it called? Or you mean the normal if else statement in javascript? Commented Nov 9, 2017 at 3:07
  • please see my answer Commented Nov 9, 2017 at 3:22
  • Looking at it, thanks :) Commented Nov 9, 2017 at 3:22

1 Answer 1

1

Here is the example using the javascript.

var test_input = $('input[name=test]');
var test_val = '';

var if_yes = '<div class="form-group"><label class="col-md-2"><b>Training Schedule:</b></label><div class="col-md-6"><input type="radio" name="training_schedule" value="Yes"> Yes<br><input type="radio" name="training_schedule" value="No"> No<br></div></div>';
var if_no = '<div class="form-group"><label class="col-md-2"><b>Training Schedule:</b></label><div class="col-md-6"><input type="radio" name="training_schedule" value="NIL"> NIL</div></div>';


$(test_input).on('change', function () {
  test_val = $(this).val();
  if (test_val == 'Yes' || test_val == 'Pending') {
    //clear the div
    $('div#append_to_this').html('');
    
    // put your html code here
    $('div#append_to_this').append(if_yes);
  } else {
    //clear the div
    $('div#append_to_this').html('');
    
    // put your html code here
    $('div#append_to_this').append(if_no);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form">
     <div class="form-group">
          <label class="col-md-2"><b>Test:</b></label>
          <div class="col-md-6">
               <input type="radio" name="test" value="Yes"> Yes<br>
               <input type="radio" name="test" value="No"> No<br>
               <input type="radio" name="test" value="Pending"> Pending<br> 
          </div>
     </div>
     <br>
     <div id="append_to_this"></div>
</div>

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

5 Comments

I also do not know how to create the html code where I would give the radio button value a fixed value. For example, name = "training_schedule" value = "NIL" under the else part, could you show me how
Just copy your form to that append. updated @blastme
is this suppose to be the location for the js? <script src="ajax.googleapis.com/ajax/libs/jquery/2.1.1/…> Or it is needed for the js to work. And also do I put it together with the form inside or in another folder? Sorry it my first time using js together with laravel
You can just write the JS directly in app.js, make sure you're running the npm run watch-poll to compile your assets. For little bits of JS like this there's no need to create new files.
No, it can be different location. You must include jQuery no matter where the location. @blastme

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.