Category Archives: javascript

Capture key press (or keydown) event on DIV element

Questions: How do you trap the keypress or key down event on a DIV element (using jQuery)? What is required to give the DIV element focus? Answers: (1) Set the tabindex attribute: <div id=”mydiv” tabindex=”1″ /> (2) Bind to keydown: $(‘#mydiv’).bind(‘keydown’, function(event) { //console.log(event.keyCode); switch(event.keyCode){ //….your actions for the keys ….. } }); To set… Read More »

CSS media queries and JavaScript window width do not match

Questions: For a responsive template, I have a media query in my CSS: @media screen and (max-width: 960px) { body{ /* something */ background:red; } } And, I made a jQuery function on resize to log width: $(window).resize(function() { console.log($(window).width()); console.log($(document).width()); /* same result */ /* something for my js navigation */ } And there… Read More »

JavaScript: Check if mouse button down?

Questions: Is there a way to detect if a mouse button is currently down in JavaScript? I know about the “mousedown” event, but that’s not what I need. Some time AFTER the mouse button is pressed, I want to be able to detect if it is still pressed down. Is this possible? Answers: Regarding Pax’… Read More »

Bootstrap close responsive menu “on click”

Questions: On “PRODUCTS” click I slide up a white div (as seen in attached). When in responsive (mobile and tablet), I would like to automaticly close the responsive navbar and only show the white bar. I tried: $(‘.btn-navbar’).click(); also tried: $(‘.nav-collapse’).toggle(); And it does work. However in desktop size, it is also called and does… Read More »

AngularJS – Building a dynamic table based on a json

Questions: Given a json like this: { “name”: “john” “colours”: [{“id”: 1, “name”: “green”},{“id”: 2, “name”: “blue”}] } and two regular html inputs: <input type=”text” name=”name” /> <input type=”text” name=”color” /> <input type=”submit” value=”submit” /> I need to build a table with all the possible variations, ex: John green John blue That means that if… Read More »

jQuery convert line breaks to br (nl2br equivalent)

Questions: I’m having jQuery take some textarea content and insert it into an li. I want it to visually retain the line breaks. There must be a really simple way to do this… Answers: demo: http://so.devilmaycode.it/jquery-convert-line-breaks-to-br-nl2br-equivalent function nl2br (str, is_xhtml) { var breakTag = (is_xhtml || typeof is_xhtml === ‘undefined’) ? ‘<br />’ : ‘<br>’;… Read More »

Setting up Twitter API, getting the last few Tweets

Questions: I am completely new to using Twitter in general and have never embedded “latest tweets” on any project. I am simply trying to embed the 3-4 newest tweets on the site footer with no additional features of functionality. I have been researching how to do this for quite some time now and having some… Read More »