www.webdeveloper.com
Results 1 to 6 of 6

Thread: Using checkboxes to remove elements

  1. #1
    Join Date
    Feb 2004
    Posts
    15

    Using checkboxes to remove elements

    I am struggling to adapt some code to my needs but there is a section that I don't understand. (I have contacted the author, but no response yet)

    Here is the sample code:

    http://viralpatel.net/blogs/2009/03/...avascript.html

    Here is the html for my own sample

    <body>
    <p>This is my introduction </p>

    <div id="firstMeal">
    <form>
    <input type="checkbox" id="firstMeal">Check to delete this meal
    </form>
    <p>This is the first meal.</p>
    </div>

    <div id="secondMeal">
    <form>
    <input type="checkbox" id="secondMeal">Check to delete this meal
    </form>
    <p>This is the second meal.</p>
    </div>

    <div id="thirdMeal">
    <form>
    <input type="checkbox" id="thirdMeal">Check to delete this meal
    </form>
    <p>This is the third meal.</p>
    </div>
    <form onSubmit="return false;">
    <input type="submit" value="Remove selected meals">
    </form>

    </body>

    And finally, here is my attempt at converting the code to meet my needs. I have placed an arrow at the section where I get lost.

    <SCRIPT language="javascript">

    function deleteMeal(divID) {
    var meal = document.getElementById(divID);
    var checkCount = document.forms.length;

    for(var i=0; i<checkCount; i++) {
    var chosen = document.forms[i];
    ---------> var chkbox = row.cells[0].childNodes[0];
    if(null != chkbox && true == chkbox.checked) {
    document.deleteMeal(i);
    rowCount--;
    i--;

    }
    }
    }
    </SCRIPT>

    Thanks for the help!

    Jean

  2. #2
    Join Date
    Feb 2004
    Posts
    15

    Another try

    I've been working on this code and finding some advice from friends, but still not getting the desired results. Any help would be appreciated. View my latest attempt here:

    http://www.whatsfordinner.net/Testing-Delete-div.html

    Thanks!

    Jean

  3. #3
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    Give me 10 mins..

  4. #4
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    Code:
         <SCRIPT language="javascript">  
       
             function deleteMeal() {    
                   var Checkboxes = document.getElementsByName('chkMeals');    
       
    	       for(var i=0; i<Checkboxes.length; i++) {  
                        var divMeal = Checkboxes[i].parentNode.parentNode;  
                        if(Checkboxes[i].checked==true){
    					divMeal.parentNode.removeChild(divMeal);   
    		   }   
                   }  
             }   
            </SCRIPT>
    There you go =)

  5. #5
    Join Date
    Feb 2004
    Posts
    15
    Thank you so much ryanlund! Of course, the if clause was left out! Well, now I'm on to my next step...setting cookies to remember the choices.

  6. #6
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    No worries =) cookies are easy enough, you wont have any trouble with them.

    good luck

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles