I have some areas of an imagemap and some checkboxes. Each area has a corresponding checkbox, how can I make it so when an area is clicked the corresponding checkbox gets ticked?
Imagemap:
<%= image_tag("maps/mainmap.png", :width => "450", :height => "450", :class => "map", :usemap => "#mainmap", :alt => "") %>
<map name="mainmap">
<area id="area-42" shape="poly"
coords="158,43,152,49,164,86,165,112,153,153,139,169,145,171,161,176,236,201,241,202,251,166,253,142,257,132,294,102,269,85,240,68,227,53,213,28,202,27" alt="North"
data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
onmouseover="document.body.style.cursor='pointer'"
onmouseout="document.body.style.cursor='default'" >
<area id="area-43" shape="poly"
coords="296,103,258,133,254,143,252,166,242,203,263,209,272,204,322,226,340,250,360,241,356,230,357,222,378,214,395,195,394,188" alt=""
data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
onmouseover="document.body.style.cursor='pointer'"
onmouseout="document.body.style.cursor='default'" >
</map>
Checkboxes:
<fieldset class="filter_form_fieldset areas">
<% Area.all.each do |a| %>
<p class="area_check"><%= check_box_tag 'areas[]', a.id, false, :id => "area-#{a.id}" %>
<label for="area-<%= a.id %>"><p1><%= a.name %></p1></label></p>
<% end %>
</fieldset>
This produces this:
<img alt="" class="map" height="450" src="/assets/maps/mainmap.png" usemap="#mainmap" width="450" />
<map name="mainmap">
<area id="area-41" shape="poly"
coords="158,43,152,49,164,86,165,112,153,153,139,169,145,171,161,176,236,201,241,202,251,166,253,142,257,132,294,102,269,85,240,68,227,53,213,28,202,27" alt="North"
data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
onmouseover="document.body.style.cursor='pointer'"
onmouseout="document.body.style.cursor='default'" >
<area id="area-42" shape="poly"
coords="296,103,258,133,254,143,252,166,242,203,263,209,272,204,322,226,340,250,360,241,356,230,357,222,378,214,395,195,394,188" alt=""
data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
onmouseover="document.body.style.cursor='pointer'"
onmouseout="document.body.style.cursor='default'" >
</map>
<fieldset class="filter_form_fieldset areas">
<p class="area_check"><input id="area-41" name="areas[]" type="checkbox" value="41" />
<label for="area-41"><p1>Chinatown</p1></label></p>
<p class="area_check"><input id="area-42" name="areas[]" type="checkbox" value="42" />
<label for="area-48"><p1>Village</p1></label></p>
</fieldset>
I thought this bit of javascript might link the two, but it's not working, any ideas as to where I'm going wrong?
$(function() {
$('area').click(function(){
var name = $(this).attr('id');
var $checkbox = $('[id=' + id + ']');
$checkbox.attr('checked', !$checkbox.attr('checked'));
});
});
Thanks very much for any help!
You are using the same id value in the <area>
tags and in the checkboxes. That will never work. You also had several errors in how you were getting the $checkbox
variable.
I changed the <area>
tags to use a data value and changed the code to retrieve that data value and it works now. You can see it in action here: http://jsfiddle.net/jfriend00/ndwjC/.
Since we don't have the actual image, one has to guess where to click to find the hotspots, but when you find one, it toggles a checkbox.
HTML:
<img alt="" class="map" height="450" src="/assets/maps/mainmap.png" usemap="#mainmap" width="450" />
<map name="mainmap">
<area data-areanum="area-41" shape="poly"
coords="158,43,152,49,164,86,165,112,153,153,139,169,145,171,161,176,236,201,241,202,251,166,253,142,257,132,294,102,269,85,240,68,227,53,213,28,202,27" alt="North"
data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
onmouseover="document.body.style.cursor='pointer'"
onmouseout="document.body.style.cursor='default'" >
<area data-areanum="area-42" shape="poly"
coords="296,103,258,133,254,143,252,166,242,203,263,209,272,204,322,226,340,250,360,241,356,230,357,222,378,214,395,195,394,188" alt=""
data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
onmouseover="document.body.style.cursor='pointer'"
onmouseout="document.body.style.cursor='default'" >
</map>
<fieldset class="filter_form_fieldset areas">
<p class="area_check"><input id="area-41" name="areas[]" type="checkbox" value="41" />
<label for="area-41"><p1>Chinatown</p1></label></p>
<p class="area_check"><input id="area-42" name="areas[]" type="checkbox" value="42" />
<label for="area-48"><p1>Village</p1></label></p>
</fieldset>
Javascript:
$(function() {
$('area').click(function(){
var name = $(this).data("areanum");
var $checkbox = $('#' + name);
$checkbox.attr('checked', !$checkbox.attr('checked'));
});
});
P.S. If you're using a recent version of jQuery, you should probably use .prop()
to change the checkbox rather than .attr()
.