Ashish Agarwal
Ashish Agarwal

Reputation: 6283

how to set default values on load in jquery form?

I am using jq-idealforms to create form. Currently I am not using dynamic method to create any field. on certian event, I would like to set value in all the fields in the given form.

In other words, I want to preload the form with specific values on certain event. I am not able to find any such kind of documentation. So need help

Demo form : http://bit.ly/1ahZalu

Upvotes: 0

Views: 1382

Answers (2)

Ian Hazzard
Ian Hazzard

Reputation: 7771

In the HTML, just use the "value" attribute for text, and the "checked" attribute for radios and checkboxes:

<input type="text" value="John"/><br/>

<input type="checkbox" checked/>

As you can see, these automatically have specified values.

I hope this helps!

Upvotes: 1

dm4web
dm4web

Reputation: 4652

I do not know if I understood well, but maybe this can help

http://jsfiddle.net/ymfvqyob/2/

    var set=[]; //array name:field value, ...
    set.push({'username':'test username 1','email':'test email 1'});
    set.push({'username':'test username 2','email':'test email 2'});

    //here is a click event used to call functions setVal    
    $('#set1').click(function(){
        setVal(0)
    })
    $('#set2').click(function(){
        setVal(1)
    })

    function setVal(ind)
    {
        $.each(set[ind],function(name,val){
            $('form input[name="'+name+'"]').val(val);
        })

    }     

Upvotes: 1

Related Questions