Understanding jQuery Mobile Buttons

07 sep. 2022
Intermediate
10,8K Views
3 min read  

Buttons are created with the help of standard HTML anchor and input elements, then enhanced by jQuery Mobile to make them more attractive and usable on a mobile device. Typically, anchor links (a elements) are used to make navigation buttons, and input or button elements are used to make buttons for form submission.

Creating a Button in jQuery Mobile

Creating a button in jQuery mobile is very simple. You can create a button in jQuery mobile by using following three HTML element:

  1. <button> element

  2. <input> element

  3. <a> element with data-role="button"

<div data-role="page">
 <div data-role="header">
 <h1>Header text</h1>
 </div>
 <div data-role="content">
 <button>Simple Button</button>

 <input type="button" value="Input Button">

 <a href="#" data-role="button">Anchor Button</a>
 </div>
 <div data-role="footer">
 <h1>Footer Text</h1>
 </div>
</div>

Adding Icons to jQuery Mobile Buttons

You can also add icons to jQuery mobile buttons by using data-icon attribute as given below:

<div data-role="page">
 <div data-role="header">
 <h1>Header text</h1>
 </div>
 <div data-role="content">
 <button data-icon="home">Simple Button</button>

 <input type="button" value="Input Button" data-icon="info">

 <a href="#" data-role="button" data-icon="search">Anchor Button</a>
 </div>
 <div data-role="footer">
 <h1>Footer Text</h1>
 </div>
</div>

By default, all icons in buttons are placed to the left. But you can also set icon positions by setting values to data-iconpos attribute as given below:

  1. Top

  2. Right

  3. Bottom

  4. Left

<div data-role="page">
 <div data-role="header">
 <h1>Header text</h1>
 </div>
 <div data-role="content">
 <button data-icon="home" data-iconpos="top">Simple Button</button>

 <input type="button" value="Input Button" data-icon="info" data-iconpos="right">

 <a href="#" data-role="button" data-icon="search" data-iconpos="bottom">Anchor Button</a>
 </div>
 <div data-role="footer">
 <h1>Footer Text</h1>
 </div>
</div>

Grouping Buttons

jQuery Mobile allows you to make a group of buttons by using data-role="controlgroup" attribute together with data-type="horizontal|vertical" to specify whether to group buttons horizontally or vertically.

<div data-role="page">
 <div data-role="header">
 <div align="center">jQuery Mobile Buttons</div>
 </div>
 <div data-role="content">
 <div data-role="controlgroup" data-type="vertical">
 <a href="#yourlink" data-role="button">Button 1</a>
 <a href="#yourlink" data-role="button">Button 2</a>
 <a href="#yourlink" data-role="button">Button 3</a>
 </div>
 </div>
</div>
What do you think?

I hope you have enjoyed the article and would able to have a clear picture about jQuery Mobile buttons. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Learn to Crack Your Technical Interview

Accept cookies & close this