Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
onclick radio button change form fields

I have two radio button like:

  1. Field_One
  2. Field_Two

When I check Field_One it will show First_Name, Last_Name field but when I check Field_Two it will show reference_id field.

But one thing if this reference_id comes from url

like myurl.com?reference_id=12345 then the radio field will auto selected and only reference field will show on form list.

My problem is when reference_id found on url Field_Two not showing it always showing Field_One on load found

Here is my snippet:

$(document).ready(function () {

    $('input[type="radio"]').click(function () {
        if ($(this).attr("value") == "Field_One") {
            $(".Field_One").show();
            $(".Field_Two").hide();
        }
        if ($(this).attr("value") == "Field_Two") {
            $(".Field_Two").show();
            $(".Field_One").hide();

        }
    });

    $('input[type="radio"]').trigger('click');  // trigger the event
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="radio" name="myfield" value="Field_One" checked/> Field_One
<input type="radio" name="myfield" value="Field_Two" /> Field_Two

<div class="Field_One">
<input type="text" name="first_name" placeholder="First Name" />
<br><br>
<input type="text" name="last_name" placeholder="Last Name"/>
</div>
<br>
<br>
<div class="Field_Two">
<input type="text" name="reference_no" placeholder="reference_no"  />
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try this:

    $(document).ready(function () {

        $('input[type="radio"]').click(function () {
            if ($(this).attr("value") == "Field_One") {
                $(".Field_One").show();
                $(".Field_Two").hide();
            }
            if ($(this).attr("value") == "Field_Two") {
                $(".Field_Two").show();
                $(".Field_One").hide();

            }
        });

        //$('input[type="radio"]').trigger('click');  // trigger the event
    
    let url = new URL(window.location.href);
    let reference_id = url.searchParams.get("reference_id");
    if (reference_id == null) {
        $(".Field_One").show();
        $(".Field_Two").hide();
    }else{
        $(".Field_Two").show();
        $(".Field_One").hide();
        $(".Field_Two").find('input[name="reference_no"]').val(reference_id);
    }

    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="radio" name="myfield" value="Field_One" checked="true" /> Field_One
<input type="radio" name="myfield" value="Field_Two" /> Field_Two

<div class="Field_One">
    <input type="text" name="first_name" placeholder="First Name" />
    <br><br>
    <input type="text" name="last_name" placeholder="Last Name" />
</div>
<br>
<br>
<div class="Field_Two">
    <input type="text" name="reference_no" placeholder="reference_no" />
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!