SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: TR setAttribute

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code JavaScript:
    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;

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably this:
    Code:
    selected.[i]
    Code:
    selected[i]
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Probably this:

    Code:
    selected[i]
    It still comes back and says it is undefined.

    Code JavaScript:
    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;

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you get if you alert here?
    Code:
     var selected= document.getElementsByTagName('tr')
     alert(selected);
    Tab-indentation is a crime against humanity.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by unemployment View Post
    It still comes back and says it is undefined.
    The loop that the selected items are used in, is determined by the number of form elements in the messages form.

    Code:
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    What do you get if you alert here?
    Code:
     var selected= document.getElementsByTagName('tr')
     alert(selected);
    The alert says [object HTMLCollection]

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The loop that the selected items are used in, is determined by the number of form elements in the messages form.

    Code:
    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?
    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.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by unemployment View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    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?

    Code PHP:
    <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>

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •