Solved

Using images as radio labels

Posted on 2011-02-24
7
317 Views
Last Modified: 2012-05-11
I want to substitute the standard radio form buttons for selectable images. Here is my markup for this...

<label><input type="radio" name="finish" value="walnut">
                            <img class="imagebox" onClick="this.className='imagebox-active';" src="awards/walnut.jpg" width="190" height="220"/></label>
                            
                            <label><input type="radio" name="finish" value="cherry">
                            <img class="imagebox" onClick="this.className='imagebox-active';" src="awards/cherry.jpg" width="190" height="220"/></label>
                            
                            <label><input type="radio" name="finish" value="piano">
                            <img class="imagebox" onClick="this.className='imagebox-active';" src="awards/piano.jpg" width="190" height="220"/></label>


Two issues:
1. How do I hide the actual radio buttons?
2. The onclick events activate the class that highlights the border to indicate selection... how do I revert back if another image is selected? Currently I can 'select' all 3 radio images.

0
Comment
Question by:pixelscape
  • 4
  • 2
7 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34972817
for all radio buttons add this style

<input type="radio" name="finish" value="walnut" style="display:none">

or you can simply remove the radio buttons itself

when you click an image, try something like

<img class="imagebox" onClick="this.className='imagebox-active';" src="awards/piano.jpg" width="190" height="220"/>

function changeClassName(thisObj)
{
   var allImages = document.getElementsByTagName("img");
   for (var counter = 0; counter < allImages.length; counter++)
   {
        allImages [ counter ].setAttribute("class", "imagebox"); //reset all images
   }
    thisObj.setAttribute("class", "imagebox-active");
}
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34972822
sorry the image markup will become
<img class="imagebox" onClick="changeClassName(this)" src="awards/piano.jpg" width="190" height="220"/>

0
 

Author Comment

by:pixelscape
ID: 34973122
Great, thanks!

var allImages = document.getElementsByTagName("img");

...applies class=imagebox-active to all images on page... I need to specify only apply to class=imagebox

Thanks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Expert Comment

by:gurvinder372
ID: 34973163
0
 

Author Comment

by:pixelscape
ID: 34973843
I am a little confused as to how this should work...

so I replace this line:
var allImages = document.getElementsByTagName("img");

with this line:
var allImages = document.getElementsByClassName('imagebox imagebox-active', 'img'); ?
0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 100 total points
ID: 34976710
if you are using the last link (second code) in my last reply, then make it

var allImages = getElementsByClassName(document, "img", 'imagebox imagebox-active' );

0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 400 total points
ID: 34978201

Try something like this:

<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26845659.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
<script> 

jQuery(document).ready(function(){

  $(".imagebox").click( function() {

    $(this).siblings().get(0).checked = true;
    $(".imagebox-active").removeClass( "imagebox-active" );
    $("input[name=finish]:checked").siblings().addClass( "imagebox-active" );

  });

});

</script>

<style>
label { display: block; cursor: pointer; }
.label-radio input { position: absolute; left: -9999px; }
.imagebox-active { border: 3px dashed #f00; }
</style>

</head>
<body>

<label class="label-radio">
  <input type="radio" name="finish" value="walnut">
  <img class="imagebox" src="awards/walnut.jpg" width="190" height="220"/>
</label>
                            
<label class="label-radio">
  <input type="radio" name="finish" value="cherry">
  <img class="imagebox" src="awards/cherry.jpg" width="190" height="220"/>
</label>
                            
<label class="label-radio">
  <input type="radio" name="finish" value="piano">
  <img class="imagebox" src="awards/piano.jpg" width="190" height="220"/>
</label>

</body>
</html>

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now