Search code examples
htmlradio-button

If I click on Radio button, name must be change


How to do if I click on radio button, name must be change. Example, if I click on Issue radio button, Security Name but be change to Recipient or if I click on Return radio button, Security Name must be change to "Return by". And I have no idea how to make it. Is there a way to do that?

<div class="col-sm-8 radio" style="margin-left: 60px;">
    <p class="form-control-static" style="display: inline-flex;white-space: break-spaces;">
        <label><input type="radio" name="status" onclick="check(this.value)" value="issue" ><b>Issue</b></label>
        <br>
        <label><input type="radio" name="status" onclick="check(this.value)" value="returnn" ><b>Return</b></label>
    </p>
  </div>

Second code is for Showing Security Name.

<label class="control-label col-sm-5" for="badgeid" style="margin-left: -50px;">Security Name:</label>
  <div class="col-sm-4">
    <p class="form-control-static" style="margin-top: -6px;">
        <input type="text" class="form-control" id="secuname" name="secuname" placeholder="Enter Name" value="<?php echo $securityname; ?>">
    </p>
  </div>
  <div class="col-sm-10"></div>
  <br>

Solution

  • Here Is Your Answer(last code section)

    <div class="col-sm-8 radio" style="margin-left: 60px;">
        <p class="form-control-static" style="display: inline-flex;white-space: break-spaces;">
        <label><input type="radio" name="status" onclick="check(this.value)" value="issue" ><b>Issue</b></label>
        <br>
        <label><input type="radio" name="status" onclick="check(this.value)" value="returnn" ><b>Return</b></label>
        </p>
    </div>
    

    <label class="control-label col-sm-5" for="badgeid" style="margin-left: -50px;">Security Name:</label>
    
    <div class="col-sm-4">
        <p class="form-control-static" style="margin-top: -6px;">
            <input type="text" class="form-control" id="secuname" name="secuname" placeholder="Enter Name" value="<?php echo $securityname; ?>">
          </p>
    </div>
    

    That's your code, now the JS you wanted in comments to do change an input's name based on another radio button input

    is here


    var statusRadioButtons = document.getElementsByName('status');
    var specialInput = document.querySelector('#secuname') ;
    statusRadioButtons.forEach( item => item.addEventListener("change",handleChangeName)) 
    function handleChangeName(ev) {
        var value = ev.target.value;
        if( value == "issue" ) {
             specialInput.setAttribute('name','receipt') ;
        } else if ( value == "returnn" ) { 
             specialInput.setAttribute('name','returnBy') ;
        } 
    }