Replacing checkboxes with images?

JulianJ

I am trying to replace three check boxes within an html form with three different images. The idea being that the user can select the pictures by clicking on them rather than clicking on a check box. I've been putting togther some code but can't figure out how to make all the checkboxes selectable. At the moment only the first images works when it is clicked on. Can anyone help me? I'm a real novice with javascript I'm afraid. See fiddle here

The form

<form id="form1" action="" method="GET" name="form1">
<div class="col-md-3">
             <img src="https://cdn2.iconfinder.com/data/icons/windows-8-metro-style/128/unchecked_checkbox.png" title="blr" id="blr"><input type="checkbox" id="imgCheck"  name="pic1" value=9></div><div class="col-md-3">
             <img src="https://cdn2.iconfinder.com/data/icons/windows-8-metro-style/128/unchecked_checkbox.png" title="blr" id="blr"><input type="checkbox" id="imgCheck"  name="pic2" value=12></div><div class="col-md-3">
          <img src="https://cdn2.iconfinder.com/data/icons/windows-8-metro-style/128/unchecked_checkbox.png" title="blr" id="blr"><input type="checkbox" id="imgCheck"  name="pic3" value=7></div>
   <input type="submit" name="submit" value="Add" />
</div>
</form>

The javascript

$('#blr').on('click', function(){
        var $$ = $(this)
        if( !$$.is('.checked')){
            $$.addClass('checked');
            $('#imgCheck').prop('checked', true);
        } else {
            $$.removeClass('checked');
            $('#imgCheck').prop('checked', false);
        }
    })
user3125999

First, as Amar said, id should be unique. Use classes for multiple elements. Also pay attention for semicolons and closing html tags.

To your question: use the function .siblings() to get the relevant checkbox element.

$('.blr').on('click', function () {
    var $$ = $(this);
    if (!$$.is('.checked')) {
        $$.addClass('checked');
        $$.siblings('input.imgCheck').prop('checked', true);
    } else {
        $$.removeClass('checked');
        $$.siblings('input.imgCheck').prop('checked', false);
    }
});

See demo here: http://jsfiddle.net/yd5oq032/1/

Good luck!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replacing checkboxes in MS-Word

Use images like checkboxes

Tampermonkey replacing words with images

Replacing images via CSS

images are not replacing while click the button

Replacing images on a page with chrome extension

Replacing images in Word 2000 doc

Replacing characters with images using HTML

Set different images for different checkboxes in a QTreeView

Checkbox functionality clicking on images instead of checkboxes

JS Tile Map - Replacing color tiles by images

Replacing images on mouseover with only childnodes and javascript

Replacing transparent background with white color in PNG images

Replacing Font Awesome Social Media Icons With Images

Preview image replacing images located in other list

Keeping selected images instead of replacing with the new ones

Why is my HTML Table resizing images when checkboxes are in my Table?

Bootstrap Multi-select - adding images after checkboxes

replacing <img> to <ion-img> and images are very small

Replacing HTML with JavaScript changed style, images no longer in-line horizontally

How to compare two identically sized images in python, replacing pixels that match between the two images with black pixels

Using checkboxes to delete multiple images from db successfully but doesn't unlink files from folder

In React, how can I create a flexible array of selected items, using checkboxes, similar to selecting images on my Phone?

Good or bad idea: Replacing all images on a mobile website with CSS property "content:url()" for retina displays

Collection of checkboxes

JTree with checkboxes

QFileSystemModel with Checkboxes

Checkboxes values

Align Checkboxes