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

167
Views
Razor show / hide base on radio button using javascript

I am trying to hide or show a textbox, in a ASP MVC razor view, now i using javascript to handle when i click the radio button which can help me to do hide or show function. however, i want to base on database record hide and show the textbox, so if i use below code , i need to click the radio for hide or show a textbox, anyone can give me advise how to hide or show textbox base on database record and no need to click radio button? thanks

<script type="text/javascript">
        const { checked } = require("modernizr");

         function text(x) {

           if  (x == 0 ) document.getElementById("name").style.display = "block",
                document.getElementById("address").style.display = "none",
               
            else  document.getElementById("name").style.display = "none",
                document.getElementById("address").style.display = "block",
            return;

        }

    </script>
   //radio name
   @Html.LabelFor(Model => Model.Type, "A")
   @Html.RadioButtonFor(Model => Model.Type, "A" , new { @onclick = "text(0)", @id = "but1" })
   
   @Html.LabelFor(Model => Model.Type, "B")
@Html.RadioButtonFor(Model => Model.Type, "B" , new { @onclick = "text(1)", @id = "but2" })

//textbox(name)
 <div class="form-group col-md-6" id="name">
  @Html.LabelFor(model => model.name, new { @class = "control-label" })
 @Html.TextBoxFor(model => model.name, new { @class = "form-control"e })
 </div>
 //textbox(address)
  <div class="form-group col-md-6" id="address">
  @Html.LabelFor(model => model.address, new { @class = "control-label" })
  @Html.TextBoxFor(model => model.address, new { @class = "form-control" })
                </div>

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

0

<script>
  // self executing function here
(function() {
  // your page initialization code here
  // the DOM will be available here
  text(@Model.Value);
})();
</script>

This is a document ready function. Your text() method will be called when page is loaded. Use @Model.Value your initial db value. That way it will work for the first time with the value which is in the db.

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!