Solved

Using images as radio labels

Posted on 2011-02-24
7
322 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can't Find the Homepage on my site 4 47
Difference between Leaflet and MapBox? 5 47
Need help for active menu class depending of the page 4 30
CSS styling problem 3 28
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article discusses how to create an extensible mechanism for linked drop downs.
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 an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

751 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