Skip to main content

Jquery Mobile Form - Text inputs

    In this post on "form text input's" styling and usage using Jquery Mobile we will cover various input types and textarea styling using JQM. We will also cover the various attributes that can be used with the input types. Text inputs and textareas are coded with standard HTML elements, then enhanced by Jquery Mobile to make them more attractive and useable on a mobile device.

    The text input elements are available in 2 flavors/sizes - regular and mini. Following is a standard HTML syntax for an input element.


    Adding a data attribute data-mini="true" to the input tag renders a smaller sized input element. Let's take a look at the code sample below to understand the working in a better way.

    Apart from the regular input type, to accept multi-line input, use the textarea element. The advantage of using textarea in Jquery Mobile is that, the framework auto grows the height of the textarea, avoiding the need for the content in the textarea to be scrolled internally. In the example below, you will see to implementations of textareas - one with label and textarea on 2 separate lines and the other with the label and textarea on the same line.



    In the code above, you will see 4 different implementations of the input element. The first one is a regular input of type text with the label and the textbox one below the other. The second implementation is exactly similar to the first implementation with the only difference of the added data attribute "data-mini="true"" which reduces the size (basically height) of the textbox.

    The third implementation gets the label and the textbox on the same line. This implementation is done using a div with data attribute data-role="fieldcontain" around the label and input tags. This div has to be within a form tag. Maintaining the HTML structure is very important when using JQuery Mobile framework.

    In the fourth implementation, we have used the HTML5 placeholder property and done away with the label completely. However, for the sake of accessibility, we have included the label tag with class="ui-hidden-accessible" so that the label is not displayed but is present in the HTML structure. This implementation is popularly used on mobile sites as this reduces the amount of scroll.

    To disable the input field just add the keyword "disabled" to the input tag and your input type would be disabled.

    A note on accessibility:
    For the sake of accessibility, jQuery Mobile requires that all form elements be paired with a meaningful label. To hide labels in a way that leaves them visible to assistive technologies — for example, when letting an element's placeholder attribute serve as a label — apply the helper class ui-hidden-accessible to the label itself. While the label will no longer be visible, it will be available to assisitive technologies such as screen readers.

    You can use the various input types supported in HTML5. The advantage of using the HTML5 input types is that keybords default to the particular input type are displayed by default. The following example demonstrates the various HTML5 input types that you can use. Try out the example on a mobile device and you will see for yourself the various keyboards that default the particular input type.



    The mobile browsers that do not support the new HTML5 input types will fall back to the default input type="text" and show the default alpha-numeric keyboard. Adding the images (screenshots) of default keyboards displayed for different input types on the Android and iOS devices, to this post would make the post really long and so I have included these images here.

    Hope this post has been informative to you and helps you use the various input types effectively in your next Jquery Mobile based application. Drop a comment to let me know what you feel about this post as well as the earlier series on Jquery Mobile listview. Share the post if is has helped you, so that maximum developers benefit from this one. Till next time, happy coding!

Comments

Recommended for You

Where does Google get it's live traffic data from?

Referring to a post that I wrote earlier, Google’s - Live traffic Layer , ever wondered how Google collected this data? I was wondering the other day, how Google received live data to display it on their maps as a layer! I looked up the web and found something very interesting and am sharing the same with you all. As we all know, the traffic layer is available most accurately in several states in USA. Most major metro areas in the US have sensors embedded in their highways. These sensors track real time traffic data. Easy to miss at high speeds (hopefully anyway, traffic permitting), more commonly noticed may be the similar sensors that often exist at many busy intersections that help the traffic lights most efficiently let the most amount of people through. The information from these tracking sensors is reported back to the Department of Transportation (DOT). The DOT uses this data to update some of the digital signs that report traffic conditions in many metro areas. They als...

Two maps on the same page - Side-by-side

    How good I am feeling to post a code example after such a long time! It's been all "news" over the past so many posts! Well now that I am finally doing a code example, I am posting a very highly requested code sample. Placing two Google Maps on the same page (Now that's simple you would say!), but side by side. Now this is the thing that most people struggle with. Well, implementing the second part is also very simple, as you will see in today's code.     Let's see the code. Here it is!     The output of the above code will be as seen in the result section above.     As most of you will realize, there are two maps, one centered at "Pune" and other at "Noida". Why I chose these two locations? Well, just like that!...The main issue of concern is how the maps appeared side-by-side and not one below the other as would be normal behavior of two "div" elements used in the same page. Now here is the trick! Check out the...

What is GIS?

          Once I started writing this blog, I received a number of feedbacks from readers asking me to write something about what exactly GIS is. So here is some information, that will give you an idea about " What is GIS? "           A geographic information system (GIS) integrates hardware, software, and data for capturing, managing, analyzing, and displaying all forms of geographically referenced information. GIS allows us to view, understand, question, interpret, and visualize data in many ways that reveal relationships, patterns, and trends in the form of maps, globes, reports, and charts.             A GIS helps you answer questions and solve problems by looking at your data in a way that is quickly understood and easily shared. GIS technology can be integrated into any enterprise information system framework.     ...

Form info window

         Today we will look at a Google Maps API v3 example to add a form in the information bubble! This is usually required when we wish to accept some data/information from the user! This data can be saved to a server in the form of an XML file or a database! The information can then be retrieved back at a later stage, when necessary!          In this example we will only look at form in the information bubble! The connectivity part with the server will be discussed in another post! So, today's code snippet is as seen below! <html> <head> <title> Google Maps API v3 - Adding marker and info window on Click and creating a form in the infowindow with the lat-lng information in it. </title> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var map;    //When using ev...

Retriving co-ordinates...

         We have seen 2 Google Maps API v3 examples wherein we have retrieved the latitude-longitude co-ordinates of the point of click on the map. In the first example we have displayed the co-ordinates in the information window and in the second , we have displayed the co-ordinates in a form in the information window.         Today we will create a code to retrieve the latitude longitude co-ordinates in a text box while simultaneously a marker appears on the map as well. So, here goes the code. <html> <head> <title> Google Maps API v3 - Adding marker on Click and retrieving the co-ordinates in a text box </title> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var map;    //When using event as a parameter to a function declare map, strictly as a global variable fun...