0

I do create a <li> like this.

<li>
<input type="checkbox" value="<?php echo $value; ?>" id="form_friend_<?php echo $value; ?>" name="form[friend][]" />
<img id="img_friend_<?php echo $value; ?>" src="">
<label for="form_friend_<?php echo $value;?>" ><?php echo $label;?></label>
</li>

How can I do the hover effect for each friend like facebook does it?

Everywhere in the cell, e.g. Allen Yee, I can activate him to send him a request. How can I realize it with my <li>?

enter image description here

Update

in head:

<script type="text/javascript">
var lis = document.getElementsByTagName('li');

for( var i = 0, l = lis.length; i < l; i++ ) {
lis[i].onclick = function() {

    var c = this.getElementsByTagName('input')[0];

    if ( c.checked )
        c.checked = false;
    else
        c.checked = true;
};
}
<script>

in body:

<li >
  <input type="checkbox" value="1" id="form_friend_1" name="form[friend][]" />
  <img id="img_friend_1" src="">    <label for="form_friend_1" >My name</label>
</li> 
Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
craphunter
  • 961
  • 2
  • 13
  • 34

2 Answers2

1

if you want all the content inside the li to trigger the checkbox to be selected, You should put all the content inside the label element.

<li>
    <label for="form_friend_<?php echo $value;?>" >
        <input type="checkbox" value="<?php echo $value; ?>" id="form_friend_<?php echo $value; ?>" name="form[friend][]" />
        <img id="img_friend_<?php echo $value; ?>" src="">
        <?php echo $label;?>
    </label>
</li>

In this case you don't even need the for attribute on the label, because the label will apply to any form control (input, select, etc.) that is inside it.

If you want to change the style of the elements inside the li when it's hovered, you could use a pseudo-class called :hover

For instance:

li:hover {
    background: #000;
}

It would change the background of the li element when it's hovered.

UPDATE

As the framework you're using doesn't allow you to wrap all li's content in the label element, I'd suggest you a solution with javascript.

Here's a simple example:

window.onload = function() {

    var lis = document.getElementsByTagName('li');

    for( var i = 0, l = lis.length; i < l; i++ ) {
        lis[i].onclick = function(e) {

            e = e || window.event;
            var el = e.target || e.srcElement;

            if ( el.nodeName == 'INPUT' ) return;

            var c = this.getElementsByTagName('input')[0];

            if ( c.checked ) {
                c.checked = false;
                this.style.background = 'white';
            } else {
                c.checked = true;
                this.style.background = 'blue';
            }

            return false;
        };
    }
}

The code above will take a list of all li elements that, when clicked, will get the first input tag inside of it and verify if it's checked, if so, it'll be unchecked, otherwise it will be checked. Also, it sets a background color for each case.

This script is simulating how an input of type checkbox would behave if it were wrapped in a label element.

UPDATE 2

As you noted in your comment bellow, the checkbox isn't working properly. It's because there's occurring a Event Bubbling. There are also several topics in Stackoverflow with deep explanation of what it is.

I updated the snippet above in order to correct the checkbox functionality.

See jsFiddle.

Hope it helps.

Community
  • 1
  • 1
rdleal
  • 987
  • 5
  • 15
  • Oh, this is working, but I can't use your example. Because symfony2 renders first the input type checkbox then the label, so I can't put it inside label. – craphunter Mar 10 '13 at 23:21
  • @Gunnar, is javascript an option? – rdleal Mar 10 '13 at 23:29
  • Oh, the javascript I posted should be executed when the DOM has loaded. See edited answer, please. I corrected the code. – rdleal Mar 10 '13 at 23:56
  • Crap, I could have seen this too! Works perfect! Thanks!!! Is it possible too, to set another background with javascript too? E.g. when I checked true then a blue background is coming up? – craphunter Mar 11 '13 at 00:01
  • Sure it's. Just use the construction `Object.style.background = 'background-declaration';`. E.g.: `this.style.background = 'blue url(imgs/xyz.jpg) no-repeat';`. See edited answer on how to apply this to the javascript I posted. – rdleal Mar 11 '13 at 00:06
  • 1
    Thanks again! Very cool and actually pretty easy. I really need to take a closer look to javascript! – craphunter Mar 11 '13 at 00:13
  • hey, you answered this question. I found out, that I do have problem with that. I can not activate the checkbox itselft, so only the litte checkbox what is 16px x 16px. Everything else can be clicked and is activating the checkbox. Any idea? – craphunter Apr 12 '13 at 09:51
  • 1
    Hi, @Gunnar. Yes, I know why and it's my bad, sorry. Take a look at the update answer, please. – rdleal Apr 12 '13 at 15:33
  • Cool, now I can click the checkbox, image and background but not the text anymore. Crap, JavaScript is pretty cool, but I don't really get it. I am more in php and c programmer. – craphunter Apr 12 '13 at 16:30
  • 1
    @Gunnar, could you please check the updated answer? Added a `return false` at the end of the event handler. – rdleal Apr 12 '13 at 18:50
0

You can do the hover effects both in css and JQuery. in css you can use '#idname:hover' or '.classname:hover'. In JQuery you can use:

$('selector that you select').hover(function(){

});

Tugkan
  • 183
  • 3
  • 12
  • This sounds pretty interesting, but I don't get it really. – craphunter Mar 10 '13 at 23:20
  • I assume you want to change the background of every box when hover. If you choose the CSS way, you can write: ".box:hover{background:#FFF;}". If you choose the JQuery way, you can write $('.box').hover(function(){$('.box).css('background-color','#FFF'}); – Tugkan Mar 10 '13 at 23:25
  • No, I want to select the box. When I hover, the background is changing, but how can I select the field with a click? – craphunter Mar 10 '13 at 23:27
  • You can do it with JQuery. The code will look like: $('.box').click(function)(){$(this).css('background-color','red');});. However this is a one-way ticket. If you click one of the boxes, it will change its background but when you click it again, it will remain same. If you want to toggle the background color on every click, you can use toggleclass() function of JQuery after defining a CSS class that changes background-color. – Tugkan Mar 10 '13 at 23:30
  • I want to toggle the background and to activate the checkbox. – craphunter Mar 10 '13 at 23:36
  • than you should look that solution: http://stackoverflow.com/questions/4243554/if-checkbox-is-checked-do-this – Tugkan Mar 10 '13 at 23:38