Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide element only if javascript is enabled

I have a view that displays a textbox as follows:

@Html.TextBoxFor(m => m.CodeGuest, new {style = "display: none"})

it is displayed via javascript (so the user clicks a checkbox).

If javascript is not enabled, I must remove this line (style = "display: none") so that the textbox is displayed by default.

like image 368
ridermansb Avatar asked Mar 03 '26 10:03

ridermansb


2 Answers

I typed this as a comment because it seemed so obvious I figured somebody else could grab some upvotes, but:

<noscript>
  <div>
    Hello YOU HAVE JAVASCRIPT TURNED OFF YOU BIG SILLY
  </div>
</noscript>

The content of a <noscript> tag is shown only when JavaScript is disabled.

like image 112
Pointy Avatar answered Mar 04 '26 22:03

Pointy


You can use client side logic for this, have it visible as default and hide it with javascript on page load.

If you use jQuery it will be something like this:

$(function() {
    $('[name="CodeGuest"]').hide();
});

And remove the display: none from the Razor logic.

Another solution is to wrap the textbox element in <noscript>-tags.

For example:

<noscript>
     @Html.TextBoxFor(m => m.CodeGuest)
</noscript>
like image 21
Robin Andersson Avatar answered Mar 05 '26 00:03

Robin Andersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!