0

Is it possible to set the background-color of the li when I put the cursor in a or b or c? I have this list with 100 records and I want the hole row ("li") to show up in another color so it gets more easy to work with the list.

<li id=recordsArray_1>
 <form medthod=post name=fr_1 id=fr_1>
  <input type=text name=a> <input type=text name=b> <input type=text name=c>
  <input type=submit name=b_1 value=ok>
 </form>
</li>

<li id=recordsArray_2>
 <form medthod=post name=fr_2 id=fr_2>
  <input type=text name=a> <input type=text name=b> <input type=text name=c>
  <input type=submit name=b_2 value=ok>
 </form>
</li>

<li id=recordsArray_3>
 <form medthod=post name=fr_3 id=fr_3>
  <input type=text name=a> <input type=text name=b> <input type=text name=c>
  <input type=submit name=b_3 value=ok>
 </form>
</li>
 .etc .... > 100
hentry
  • 33
  • 1

4 Answers4

2

You can have a sibling element to the input elements and make it cover the li with background color on hover.

CSS

input[type=text]:focus~div {
    background-color:red;
    position: absolute;
    left: 0px;
    right: 0px;
    top: 0px;
    bottom: 0px;
    z-index: -1;
}
li {
    position:relative;
}

jsfiddle

Mr_Green
  • 36,985
  • 43
  • 143
  • 241
0

You can't do this with pure css, you'll need to use javascript / jQuery:

$("input[type='text']").focus(function(){
    $("li").removeClass("background");
    $(this).closest("li").addClass("background");
});

See: http://jsfiddle.net/HF5Tx/

Leon
  • 1,649
  • 16
  • 33
  • I can see your jsfiddle working just fine, but I can't make my script work. My li looks like this: #contentLeft li { text-align:left; list-style: none; margin: 0 0 4px -40px; padding: 2px; border: #CCCCCC solid 1px; background-color:#FFFFEF; color:#000; width:1160px; } And the code from you: – user3214817 Feb 04 '14 at 14:59
  • In your javascript, change $("contentLeft li") to $("#contentLeft li"). And make sure you add a background class in your css, i.e: .background { background-color:red; }. I used the "background" class as an example, you can change it to "active" or "li-active", or something (in both your jQuery code and your CSS of course). – Leon Feb 04 '14 at 15:05
  • Thank you! Now the lines get red, BUT they all stay red after clicking so I can have many red lines and not only the active as in your example. Have I missed something? – user3214817 Feb 04 '14 at 15:44
  • Make sure you have the right selector in $("#contentLeft li").removeClass("background"). In your example you missed the "#". Make sure the list-items ("li" elements) are inside an element with the id "contentLeft". Or do $("li").removeClass("background") to target all list-items on the page. – Leon Feb 04 '14 at 18:31
  • I broke my page down in pieces and the reson it doesn't work is cause I have a table within my
  • ...
  • . Can this be fixed or do I have to rebuild my hole webpage. I have 40 cols (containg text-inputs checkbox inputs) in each row so it would take a lot of work to place them all with css. Can this script work if I click a checkbox as well as text input box? – user3214817 Feb 05 '14 at 07:32
  • Try changing $(this).parent().parent().addClass("background"); with $(this).closest("li").addClass("background"); If that doesn't work, please post a Fiddle of your code. – Leon Feb 05 '14 at 08:57
  • Yes, that works! Thank you. But is it possible to use checkboxes in the same function? – user3214817 Feb 05 '14 at 09:49
  • EDIT: I doubbled the function and put checkbox in the other and that works. Thank you very much for all your help – user3214817 Feb 05 '14 at 10:04
  • You're welcome :) Btw you can change $("input[type='text']") to $("input[type='text'], input[type='checkbox']") to target textfields and checkboxes, or just $("input") to target all input elements. – Leon Feb 05 '14 at 10:29
  • Thank you for all your help:) – user3214817 Feb 05 '14 at 12:56