Creating a jQuery object from a big HTML-string

Update: From jQuery 1.8, we can use $.parseHTML, which will parse the HTML string to an array of DOM nodes. eg: var dom_nodes = $($.parseHTML(‘<div><input type=”text” value=”val” /></div>’)); alert( dom_nodes.find(‘input’).val() ); DEMO var string = ‘<div><input type=”text” value=”val” /></div>’; $(‘<div/>’).html(string).contents(); DEMO What’s happening in this code: $(‘<div/>’) is a fake <div> that does not exist … Read more

getAttribute() versus Element object properties?

getAttribute retrieves the attribute of a DOM element, while el.id retrieves the property of this DOM element. They are not the same. Most of the time, DOM properties are synchronized with attributes. However, the synchronization does not guarantee the same value. A classic example is between el.href and el.getAttribute(‘href’) for an anchor element. For example: … Read more

How can I detect whether an iframe is loaded?

You may try this (using jQuery) $(function(){ $(‘#MainPopupIframe’).load(function(){ $(this).show(); console.log(‘iframe loaded successfully’) }); $(‘#click’).on(‘click’, function(){ $(‘#MainPopupIframe’).attr(‘src’, ‘https://heera.it’); }); }); <script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> <button id=’click’>click me</button> <iframe style=”display:none” id=’MainPopupIframe’ src=”” /></iframe> jsfiddle DEMO. Update: Using plain javascript window.onload = function(){ var ifr = document.getElementById(‘MainPopupIframe’); ifr.onload=function(){ this.style.display=’block’; console.log(‘laod the iframe’) }; var btn = document.getElementById(‘click’); btn.onclick=function(){ ifr.src=”https://heera.it”; }; … Read more

CSS media queries for screen sizes

Put it all in one document and use this: /* Smartphones (portrait and landscape) ———– */ @media only screen and (min-device-width : 320px) and (max-device-width : 480px) { /* Styles */ } /* Smartphones (landscape) ———– */ @media only screen and (min-width : 321px) { /* Styles */ } /* Smartphones (portrait) ———– */ @media … Read more

Looping through localStorage in HTML5 and JavaScript

You can use the key method. localStorage.key(index) returns the indexth key (the order is implementation-defined but constant until you add or remove keys). for (var i = 0; i < localStorage.length; i++){ $(‘body’).append(localStorage.getItem(localStorage.key(i))); } If the order matters, you could store a JSON-serialized array: localStorage.setItem(“words”, JSON.stringify([“Lorem”, “Ipsum”, “Dolor”])); The draft spec claims that any object … Read more

Is well formed without a ?

<input> without a <form> appears valid, yes (at least for html 4.01, look near the end of 17.2.1): The elements used to create controls generally appear inside a FORM element, but may also appear outside of a FORM element declaration when they are used to build user interfaces. This is discussed in the section on … Read more

How to vertically align into the center of the content of a div with defined width/height?

I have researched this a little and from what I have found you have four options: Version 1: Parent div with display as table-cell If you do not mind using the display:table-cell on your parent div, you can use of the following options: .area{ height: 100px; width: 100px; background: red; margin:10px; text-align: center; display:table-cell; vertical-align:middle; … Read more