TR setAttribute

Firebug says selected is undefined. Do you know why? I want it to add the class selected to all of the tr’s in my table when the checkbox is checked. Everything else in the function works.


function selectall () {
	var messages= document.getElementById('pm');
	var selected= document.getElementsByTagName('tr')
	var i =0;
	
	if (checked === false)
		{
			checked = true;
			document.getElementById('multiple_action').disabled = false;
			document.getElementById('drop_button').setAttribute("class", "drop_button");
		}
        else
        {
			checked = false;
			document.getElementById('multiple_action').disabled = true;
			document.getElementById('drop_button').setAttribute("class", "drop_button disabled");
        }
	for (i; i < messages.elements.length; i++) 
	{
		messages.elements[i].checked = checked;
		selected.[i].setAttribute("class", "selected");
	}
}
checked=false;

Probably this:

selected.[i]
selected[i]

It still comes back and says it is undefined.


function selectall () {
	var messages= document.getElementById('pm');
	var selected= document.getElementsByTagName('tr')
	var i =0;
	
	if (checked === false)
		{
			checked = true;
			document.getElementById('multiple_action').disabled = false;
			document.getElementById('drop_button').setAttribute("class", "drop_button");
		}
        else
        {
			checked = false;
			document.getElementById('multiple_action').disabled = true;
			document.getElementById('drop_button').setAttribute("class", "drop_button disabled");
        }
	for (i; i < messages.elements.length; i++) 
	{
		messages.elements[i].checked = checked;
		selected[i].setAttribute("class", "selected");
	}
}
checked=false;

What do you get if you alert here?

 var selected= document.getElementsByTagName('tr')
[B] alert(selected);[/B]

The loop that the selected items are used in, is determined by the number of form elements in the messages form.


var messages= document.getElementById('pm');
var selected= document.getElementsByTagName('tr')
...
for (i; i < messages.elements.length; i++)  {
    messages.elements[i].checked = checked;
    selected[i].setAttribute("class", "selected");
}

How do those numbers compare? The number of elements in the messages form versus the number of tr elements on the page?

The alert says [object HTMLCollection]

These numbers are equal. Each <tr> has a checkbox. Essentially, I am making a private messaging system and each message has a checkbox. This is the function for the select all checkbox. But I want to be able to style each tr that is selected.

You may have to put up on to the web a test version of the page that demonstrates the problem, so that a deeper investigation can occur.

This is essential what the form looks like. Does this help?


<form method="get" id="pm" name="pm" onsubmit="return false;">
	<table class="messages">
		<?php
		     $even_odd = true;
		     foreach ($messages as $message)
		     {
			$even_odd = ($even_odd) ? false : true;				
			     ?>
			     <tr>
			           <td class="box_column">
                                        <label>
                                              <input type="checkbox" id="msgcheckbox" name="pmcheckbox" onclick="selectone();" value="pm<?php echo $message['message_id']; ?>" />
                                         </label>
                                    </td>
			     </tr>
          </table>
</form>

The reason why the problem is happening is that the script is picking up from the form more elements than you have table rows on which to act upon.

Why that is happening is not possible to determine without investigating more of the HTML code.

Unless you can provide a sample test page, such further investigation cannot occur.