0

I have the function below that I am trying to make it fill a form with a specified ID.

function input_form(form, data_array)
{
$.each(data_array, function(key, value){
    //alert(key + " = " + value);
    $(form).find('input[name="'+key+'"]').each(function()
    {
        switch(this.type)
        {
            case 'select-one':
            case 'select-multiple':
                $('input[name="'+key+'"] value:contains("'+value+'")').attr('selected','selected');
                break;
            case 'password':
            case 'text':
            case 'textarea':
                //alert(key + " = " + value);
                $('input[name="'+key+'"]').val(value);
                break;
            case 'checkbox':
            case 'radio':
                $('input[name="'+key+'"] value:name("'+value+'")').attr('checked','checked');
            //this.checked = true;
        }
    });
});
}

I have the following problems:

  1. It fills in text boxes fine, but dont work for radios and checkboxes.

  2. I have another form with the same name values and its also filling in that form even tho the form has a different ID.

var form = '#form1'

var data_array = the keys are the name values of the form to be filled in by the values.

Can anyone see where I am going wrong or if it has anything else wrong with it.

Thanks.

SOLUTION

Thanks to Matt BAll for cleaner code. I edited for radio and checkboxes as it was selecting the last radio instead of actual selected one.

function input_form(form, data_array)
{
    $.each(data_array, function(key, value){
        //alert(key + " = " + value);
        $(form).find('input[name="'+key+'"]').each(function()
        {
            var $this = $(this);
            switch(this.type)
            {
                case 'select-one':
                case 'select-multiple':
                    $this.attr('selected', true);
                    break;
                case 'password':
                case 'text':
                case 'textarea':
                    $this.val(value);
                    break;
                case 'checkbox':
                case 'radio':
                    //
                    if($(this).val() == value)
                    {
                        this.checked = true;
                    }
            }
        });
    });
}

1 Answer 1

1

Try this on for size.

function input_form(form, data_array)
{
    $.each(data_array, function(key, value){
        //alert(key + " = " + value);
        $(form).find('input[name="'+key+'"]').each(function()
        {
            var $this = $(this);
            switch(this.type)
            {
                case 'select-one':
                case 'select-multiple':
                    $this.attr('selected', true);
                    break;
                case 'password':
                case 'text':
                case 'textarea':
                    $this.val(value);
                    break;
                case 'checkbox':
                case 'radio':
                    this.checked = true;
            }
        });
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the cleaner code I edited for radio and checkboxes (see my post).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.