Before moving on to the answer, I must mention security. You probably know, but beneficial for future readers ... you should not rely on simply hiding or not creating HTML elements as a means of restricting user access. Why - because when your form submits data to the server, this can be observed with simple tools like Fiddlr. If you just hide important inputs on a web page, but send them anyway, then a mischievous user can use something like Postman to edit and send dodgy values ββto your server. INCLUDING for fields that were not visible on the form. Even if you have server-side code to restrict HTML generation for limited input, if the user can see the full submission form, or, more likely, if your API is self-descriptive or well-documented, then they can start the mail manager and start sending your server all sorts of available data.
For this reason, it is very important that you re-check the user, your role and the right to change the server on each interaction. Sermon.
Assuming that the above protection is installed, the path forward is relatively simple. In your HTML, you assign classes to the elements to be displayed / hidden, and you send the variable from the server to dictate their hidden or visible state.
For example, let's say you have two user groups called usrNormal and usrAdmin. You may have an input defined as:
<input type="text" class="usrNormal usrAdmin" name="somedata" style="display:hidden;"></input> <input type="text" class="usrAdmin" name="admindata" style="display:hidden;"></input> <div class="usrNormal usrAdmin" style="display:hidden;">Some important info here....</div> <div class="usrAdmin" style="display:hidden;">Some admin-only info here.... </div>
The key to this method is to set the css class="usrNormal usrAdmin"
And the accompanying JS function:
var usrType = "usrNormal"; function protect() { $("." + usrType).show(); } protect();
I used jQuery here in a line inside a function, you can use simple JS to achieve the same. We launch a page with hidden important inputs, divs and other html elements. The usrType parameter comes from the server, and when the protect () function is called, it finds all the elements with the given user type class and makes them visible.
EDIT: see working snippet below. Although the far-fetched idea is clear, I hope. One point to consider is the use of a generic marker class to indicate that the relevant elements should be involved in the show / hide operation. Let me know if you have any questions.
$( document ).ready(function() {
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form name="form1"> <p> <span>This selection simulates the value passed from the server. When changed the elements with the matching classes are shown or hidden.</span><br /> </p> <p> <span> <input type="radio" class="serverVal" id="usrType1" name="usrType" value=".usrNormal" checked="1" /> <label for="usrType1"> Normal User only</label> </span> <span> <input type="radio" class="serverVal" id="usrType2" name="usrType" value=".usrAdmin"/> <label for="usrType2"> Admin User only </label> </span> <span> <input type="radio" class="serverVal" id="usrType3" name="usrType" value=".usrAdmin, .usrNormal" name="usrType3"/> <label for="usrType3"> Both </label><br /> </span> </p> <hr> <p class="usrNormal usrAccess" style="display:none;"> <label for="somedata">Normal only</label> <input type="text" name="somedata" /> </p> <p class="usrAdmin usrAccess" style="display:none;"> <label for="admindata1">Admin only</label> <input type="text" class="usrAdmin" name="admindata1" /> </p> <p class="usrNormal usrAccess" style="display:none;"> <label for="someinfo">Info 1</label> <textarea id="someinfo">This textare is visible to normal users...</textarea> </p> <p class="usrAdmin usrAccess" style="display:none;"> <label for="admininfo">Info 2</label> <textarea id="admininfo">This textare is visible to only Admin users...</textarea> </p> </form>