Category: JQuery
JQuery script to detect Android device
I needed to create a way of detecting whether my application was running on an Android device, as an Android device does not cope with the command ‘window.print()‘, although the user may still go to the Android browser menu and click print to reveal the same option.
The way I did this was to create the following script:
$(document).ready(function() { var ua = navigator.userAgent.toLowerCase(); var isAndroid = ua.indexOf("android") &gt; -1; // If Android then do something if (isAndroid) { // Find class <strong>.printButton</strong> and add 2 new styles to it $(".printButton").css({ "visibility": "hidden", "display": "none" }); } });
The CSS for .printButton before the script runs looks like this:
.printButton { font-size: 16px; float: left; color: black; }
After the script runs, .printButton acts like this:
.printButton { visibility: hidden; display: none; font-size: 16px; float: left; color: black; }
jQuery Highlight Table Row ID
Covering a couple of issues which I encountered.
- How to Highlight a row in a Table on click using jQuery
- How to Only use the above code on that Table
Code:
$(document).ready(function() {
$("table#SearchResultsTable").on('click', 'tr', function () {
var state = $(this).hasClass('highlighted');
$('.highlighted').removeClass('highlighted');
if (!state) {
$(this).addClass('highlighted');
}
});
});
Code to use just your desired Table:
$("table#SearchResultsTable")
Hope the above helps you on your travels.
Simple JQuery Onclick within Visual Studio
I wanted to perform what I thought would be a simple task..
Select a Radio button and make this perform its task without submitting the form with an extra click on a button.
After much searching and trial and error I found that this task can be done with a simple extension to the radio button:
onClick = "$(this).closest('form').submit();"
Here it is in context:
@Html.Label("Student")
@Html.RadioButtonFor(model => model.RequiredFilter, "Student", new { @class = "radioAlignLeft", onClick = "$(this).closest('form').submit();" })
Hope it helps, if I had found this blog post it would have helped me!