Allowing only Alphanumeric values [duplicate]

The better solution might be to go with regular expression based checks. Example below will limit only alphanumeric characters in the field with id text: $(‘#text’).keypress(function (e) { var regex = new RegExp(“^[a-zA-Z0-9]+$”); var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); if (regex.test(str)) { return true; } e.preventDefault(); return false; });

jQuery find input type (but also for select)

You can do this (fiddle here), make some sort of easy to use plugin: $.fn.getType = function(){ return this[0].tagName == “INPUT” ? this[0].type.toLowerCase() : this[0].tagName.toLowerCase(); } And use it like this $(“.element”).getType(); // Will return radio, text, checkbox, select, textarea, etc (also DIV, SPAN, all element types) $(“.elList”).getType(); // Gets the first element’s type Which … Read more

Scale div with its content to fit window

let outer = document.getElementById(‘outer’), wrapper = document.getElementById(‘wrap’), maxWidth = outer.clientWidth, maxHeight = outer.clientHeight; window.addEventListener(“resize”, resize); resize(); function resize(){let scale, width = window.innerWidth, height = window.innerHeight, isMax = width >= maxWidth && height >= maxHeight; scale = Math.min(width/maxWidth, height/maxHeight); outer.style.transform = isMax?”:’scale(‘ + scale + ‘)’; wrapper.style.width = isMax?”:maxWidth * scale; wrapper.style.height = isMax?”:maxHeight * scale; … Read more

How to set a radio input as checked using jQuery

$(‘.checkbox’).attr(‘checked’,true); will not work with from jQuery 1.6. Instead use $(‘.checkbox’).prop(‘checked’,true); $(‘.checkbox’).prop(‘checked’,false); Full explanation / examples and demo can be found in the jQuery API Documentation https://api.jquery.com/attr/

Priority when more than one event handler is bound to an element via JQuery

I want to point out that the “First come, first serve” rule is not always true, it also depends on how you register a handler: Handler1 – $(document).on(‘click’, ‘a’, function….) Handler2 – $(‘a’).on(‘click’, function….) This the above example, the Handler 2 is always called before the handler1. Have a look at this fiddle: http://jsfiddle.net/MFec6/

CSS overflow-x hidden and overflow-y visible

You can do this with CSS like this: HTML: <div class=”wrapper”> <div class=”inner”> </div> </div> CSS: .wrapper{ width: 400px; height: 300px; } .inner{ max-width: 100%; overflow-x: hidden; } Now your .wrapper div will have overflow: visible; but your .inner div will never overflow because it has a maximum width of 100% of the wrapper div. … Read more

tech