Skip to main content

Posts

Showing posts with the label javascript

Using HTML5 SessionStorage in forms.

The two local storage mechanisms in HTML5 , 'LocalStorage' and 'SessionStorage' are really nifty when you need to store the data temporarily but don't want to involve any server side interaction. I wanted to create a small HTML-form page which would allow user to enter data in various fields and on submitting the form, just show the data submitted in a text-format (easy for copy-paste). And if needed allow the data to be edited by repopulating the data in the fields. Something like this: On submitting, the entered data shows up like this: On reloading the page, once again we get the first page with data populated in the fields. Pretty cool eh? I decided to use sessionStorage for this. Idea is very simple (and you will notice that implementation is as simple!). Before starting with sessionStorage or JS code, let's take a look at the form HTML code: <form id="bct" > < label>Problem Description/Current Behavior: < /l...

Javascript : How to setfocus on an element created at runtime?

In my web page, I created a new row in my data table at runtime. The new row consisted of two text boxes and I needed to set focus on first text box in the row. I got the id of the text box element by inspecting the HTML code and tried to do the following: In java code: inputId = ; executeJavaScript("selectDPTypeRow('"+inputId+"')" );   ('executeJavaScript' is a custom method to execute JS code from Java code) In JS code: function selectNewRow(textBoxElementId){        var textBox = document.getElementById(textBoxElementId);        textBox.setFocus(); } But this never worked as 'textBox' always gave null. I was baffled as to why should the element be not found in the page DOM since inputId was always correct. Later I realized that by the time the above code is called, the new elements have not yet been rendered on the page and hence the elements are not found. To overcome this I introduced a timelag of half a second...