Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using images as radio labels

Posted on 2011-02-24
7
320 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 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