views:

152

answers:

4

I have this code on javascript side:

function changeSelectionStyle(id) {
    if(document.getElementById(id).className != 'yes'){
    document.getElementById(id).className = 'yes';
    }
    else{document.getElementById(id).className = '';}
}

And this on html:

    <ul>
      <li id="selectedAuthorities-4_1li" class="">
        <input type="checkbox" id="selectedAuthorities-4_1" name="selectedAuthorities" value="ROLE_ADD_COMMENT_TO_CV" checked="checked" onload="changeSelectionStyle(this.id + 'li');" onclick="changeSelectionStyle(this.id + 'li'); checkFatherSelection(this.id);">

        <a href="#" onclick="document.getElementById('selectedAuthorities-4_1').click(); return false;">  Agregar comentario <samp><b></b>Permite agregar comentario en el detalle.</samp>
        </a>
     </li> 
  </ul>

Well what I cant get work is when the checkbox is selected, on load, the JS should check it, and assign class="yes" to the li, here: <li id="selectedAuthorities-4_1li" class="">

Any suggestions? Thank you

+1  A: 

Looks like you're using the wrong id to reference the LI within the anchor's onclick handler, try:

document.getElementById('selectedAuthorities-4_1li').click();
karim79
I have it onclick already, check html. I need an example to update my JS to check onLOAD if the checkbox is checked and in that case, do className='yes'
BoDiE2003
A: 

There are two ways to go about dealing with this very common situation:

  1. Initialize the class with server-side logic according to the server-side data that drives the setting of the checkbox. Sometimes this is easy, sometimes it isn't.

  2. Drop in a little <script> block that triggers the "click" operation. That's sort-of a pain because you want to trigger the click without the checkbox toggling; exactly how you do that depends on the way your events are set up in general, but in your case it really just has to call the function.

Your "changeSelectionStyle" function really should be making the decision of what the class name should be by first inspecting the checkbox directly, in my opinion. Perhaps the checkbox "checked" setting could be a separate parameter to the function.

Pointy
A: 

You are using onload on an input element which is not a valid event for that type of element. You will need to use the onload on the body element:

<body onload="changeSelectionStyle('selectedAuthorities-4_1li');">

Obviously in your real app you'll have to loop for all your LI. Using jQuery for this would make it much simpler

SBUJOLD
A: 

This is the ugliest piece of code I've written since the time I switched to jQuery:

function update_checkbox() {
    if (document.getElementById('selectedAuthorities-4_1li').checked) {
        document.getElementById('selectedAuthorities-4_1li').parentNode.className = 'yes';
        // or use your changeSelectionStyle() 
    }
}
CrossAttachEvent(window, 'load', update_checkbox);
function CrossAttachEvent(o, e, f) {
    if (o.addEventListener) {
        o.addEventListener(e, f, false);
    }
    else if (o.attachEvent) {
        o.attachEvent("on" + e, f);
    }
}

CrossAttachEvent is a function I "derived" from an example on the web. It is supposed to be a cross-browser function for appending events to objects, in this case, onload event of the window.

Salman A
I have it onclick already, check html. I need an example to update my JS to check onLOAD if the checkbox is checked and in that case, do className='yes' –
BoDiE2003
I modified my answer.
Salman A