tags:

views:

61

answers:

2

Hello all,

I'm trying to achieve a fairly easy triggering mechanism for deleting multiple items from a tablegrid. If a user has enough access he/she is able to delete multiple users from a table. In the table I have set up checkboxes, one per row/user. The name of the checkboxes is UsersToDeletep[], and the value per row is the unique UserID.

When a user clicks the button 'Delete selected users' a simple validation takes place to make sure at least one checkbox is selected. After that I call my simple function Submit(form). The function works perfectly when called within the form-tags, where I also use it to delete a single user.

The function:

function Submit(form)
{   
    document.forms[form].submit();
}

I've also alerted document.forms[form]. The result is, as expected [object HTMLFormElement]. But for some reason the form just won't submit and a pagereload takes place. I'm a bit confused and can't seem to figure out what I'm doing wrong.

Can anyone point me in the right direction?

Thanks in advance!

Ben

As requested: the calling-code:

The code placed within 'onclick="javascript:"':

if(CheckMinimumCheckedCheckboxes('FormUserList', 1) == true)
{ 
    if(confirm('Weet u zeker dat u de geselecteerde gebruiker(s) wilt verwijderen?')) 
    { 
        Submit('FormUserList'); 
    } 
} 
else 
{ 
    alert('U dient minimaal 1 gebruiker te selecteren welke u wilt verwijderen.'); 
}

Seperate functions in an external JS-file.

function CheckMinimumCheckedCheckboxes(formName, minAmount)
{
    var totalChecked = 0;
    var totalElements = document.forms[formName].length;

    for(i = 0; i < totalElements; i++)
    {
        if(document.forms[formName][i].checked == true)     
        {
            totalChecked++;
        }
    }

    if(totalChecked >= minAmount)
    {
        return true;
    }

    return false;
}

function Submit(form)
{   
    document.getElementById(form).submit();
}
+1  A: 

Try using the getElementById() Method:

http://www.w3schools.com/jsref/met_doc_getelementbyid.asp

document.getElementById("frm1").submit();

http://www.w3schools.com/jsref/met_form_submit.asp

gurun8
Thanks for your answer. But it didn't resolve in a working solution.
Ben Fransen
You need to add an ID attribute to your form tag.<form name="frm1" id="frm1"> and then call that name in the getElementById() method. The document.getElementById(form).submit(); won't work unless you have a var named form. If you name your form "form" then you need to reference it in the getElementById() mehtod as string document.getElementById("form").submit(); It looks like you referenced it as a scalar variable.
gurun8
I pass it into the function (called from the page). then in the external js-file the functionparameter is passed into getElementById(parameterName). And ofcourse I've added an ID attribute to the form ;) But, it didn't work.
Ben Fransen
You may also try using jQuery to make your life a little easier. You'd iterate through your forms using the .each() method: $("form").each(...);http://api.jquery.com/each/and call the .submit() method on each reference http://api.jquery.com/submit/
gurun8
Thanks for afterreplying with a helpfull tip! +1 for your answer and comment. Thanks for the effort you made!
Ben Fransen
A: 

Never, ever, ever place an empty 'href=""' in your links and think you will fill it in later while later you decide to handle the link-element by an onclick-eventhandler... Seriously, it can save you hours of silly testing. :/

Ben Fransen