26. November 2008 by Janko in Tutorials | tags: ,

If you ever saw how products like Microsoft CRM look like you probably noticed there are input fields that have "embedded" image buttons inside them. If your clients saw that, there is a probability that they will want to have it in their applications.

Whether you agree or not, here is how you can do it easily. So easily that you will have to add just a few lines of code and enable this feature in entire application.

Download source code  View live demo

If you want to add some functionality to some input field (like adding a calendar icon that will popup some calendar), you would probably do it like on the image below.

outside_the_box  

The code could look like this:

<fieldset id="sampleForm">
    <label for="txtName">Full name</label>
    <input id="txtName" type="text" />
    <label for ="txtDOB">Date of birth</label>
    <input id="txtDOB" type="text" class="inputWithImge" />
    <img src="calendar.png" alt="Click to popup the clendar!" onclick="alert('Popup some calendar here!');" />
    <label for="txtCity">City</label>
    <input id="txtCity" type="text" />
    <label for="txtPostCode">Post code</label>
    <input id="txtPostCode" type="text" />
    <label for="txtCountry">Country</label>
    <input id="txtCountry" type="text" />
    <label for="txtBank">Bank</label>
    <input id="txtBank" type="text" class="inputWithImge" />
    <img src="bank.png" alt="Click to popup the bank window!" onclick="alert('Popup some window here!');" />
</fieldset>
<button id="btnNothingToDo">Do nothing</button>

And we could have CSS classes like these:

body { font-family: Arial, Helvetica, Sans-serif; font-size:13px;}
#sampleForm label { display:block; margin-top:10px;}
#sampleForm input[type="text"] { width:200px; border:solid 1px #000; padding:3px 0px;}
#sampleForm img { vertical-align:middle; cursor:pointer; }

However, your clients want it to be like on the image below. Image button has to be a part of an input field and everything has to be aligned.

inside_the_box

So what could you do? You can wrap input field and image button in some div and set black border to that wrapper div. You will also have to remove border form input field, and make it a little bit shorter, so that image can fit inside the wrapper div.

schema

But if you work on applications that have tens of forms, there is a probability you will have to do this tens or even hundreds of times. Insane, really.

But, remember jQuery?

You can add just one line of the code to make it work on all of your pages. Ok, here is the explanation so pay attention because this might sounds tricky.

We want to find a set of two elements (input field and image) that repeats in the fieldset and wrap it inside the div. How to find these sets? This is what jQuery will do. We'll assign a class to each input field that we want to wrap. That class will also remove border form input field. So, here is the HTML code:

<fieldset id="sampleForm">
    <label for="txtName">Full name</label>
    <input id="txtName" type="text" />
    <label for ="txtDOB">Date of birth</label>
    <input id="txtDOB" type="text" class="inputWithImge" />
    <img src="calendar.png" alt="Click to popup the clendar!" onclick="alert('Popup some calendar here!');" />
    <label for="txtCity">City</label>
    <input id="txtCity" type="text" />
    <label for="txtPostCode">Post code</label>
    <input id="txtPostCode" type="text" />
    <label for="txtCountry">Country</label>
    <input id="txtCountry" type="text" />
    <label for="txtBank">Bank</label>
    <input id="txtBank" type="text" class="inputWithImge" />
    <img src="bank.png" alt="Click to popup the bank window!" onclick="alert('Popup some window here!');" />
</fieldset>
<button id="btnNothingToDo">Do nothing</button>

and this is what jQuery has to do:

$(document).ready(function() {
    $(".inputWithImge").each(function(){
        $(this).add($(this).next()).wrapAll('<div class="imageInputWrapper"></div>');
    });
});

All that is left to do is to add two more classes that will define styles for our wrapper div and input field that will be wrapped.

.imageInputWrapper{ width:200px; border:solid 1px #000;  }
#sampleForm input.inputWithImge { width:175px; border:none; margin-right:5px;}

Download source code  View live demo

Pretty simple, eh? Maybe. Maybe this could be done even simpler. Any ideas?

If you liked this article why don't you share it:

Make image buttons a part of input fields (via @jankowarpspeed) Share this on StumbleUpon Share this on delicious Share this on Digg Share this on Dzone Share this on DesignBump Send this to friend

Comments

Pingbacks and trackbacks

  1. Trackback from Design Bump Story on DesignBump.com
  2. Pingback from addicottweb.com Style Your Form Fields with Icons to Improve Usability - Usability Article by Addicott Web, Chicago Web Design & Web Consulting
  3. Pingback from speckyboy.com 20+ Resources and Tutorials for Creative Forms using CSS : Speckyboy Design Magazine
  4. Pingback from guidesigner.net Best Tips, Resources and Tutorials for making of Creative Forms using CSS | guidesigner.net
  5. Pingback from answerspluto.com list of urls - 5 « Answers Pluto
  6. Pingback from tripwiremagazine.com 60+ Must Have jQuery Toolbox | tripwire magazine
  7. Pingback from muliati.com 60+ Must Have jQuery Toolbox | The Muliati.Com goBlog
  8. Pingback from tripwiremagazine.com 95+ Exceptionally Useful jQuery Plugins, Tutorials and Cheat Sheets | tripwire magazine
  9. Pingback from millionaire.websitesuperhero.com Make image buttons a part of input fields
  10. Pingback from huibit05.com 50+ jQuery Plugins for Form Enhancements | huibit05.com
  11. Pingback from resourcesmix.info Web Form Enhancement with jQuery Plugins and Tools | ResourcesMix.info | Huge collection of design and development tips, tutorials, downloads and news. actionscript, ajax, banking, brushes, business, css, design, download, downloads, Featured, flash, fonts, Freelance, Graphic, html, icon, illustrator, inspiration, javascript, photos, photoshop, resources, texture, textures, theme, tips, tools, tutorials, vector, Wallpapers, Web design, wordpress
  12. Pingback from vietphotoshop.wordpress.com 95+ Exceptionally Useful jQuery Plugins, Tutorials and Cheat Sheets « Photoshop.vn – Your Design Resource
  13. Pingback from neurosoftware.ro 75+ Top jQuery Plugins to improve Your HTML Forms | Programming Blog
  14. Pingback from afiffattouh.com 75+ Top jQuery Plugins to improve Your HTML Forms | Afif Fattouh - Web Specialist
  15. Pingback from booto.net 25个表单jQuery插件 « Booto'Blog
  16. Pingback from sev7en.com 25个表单jQuery插件 | 尚7科技
  17. Pingback from bianchient.com 30+ jQuery Plugins to Improve Usability and Look and Feel of HTML Forms - Memphis, Tennessee's Premier Web Design Agency

Add comment

   
        Country flag
biuquote
Loading