Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:43:17+00:00 2026-06-13T09:43:17+00:00

I have a form that submits values to database via Ajax and works fine

  • 0

I have a form that submits values to database via Ajax and works fine but I also need to give option to user to reset the form to its default values in case they make a mistake.

The default values are stored in a javascript variable as json object

{"field_name1":"2","field_name3":"3","field_name3":"1000"...

problem I have is that the form has multiple input types , textarea , select , radio
and I need to figure out what they are based on the object key , look for name and return type , so I could do something like if radio set checked checked and so on

I tried

Object.each(dafaultValues, function(value, key){
 var filed_name = MyForm.elements[''+key+''];
 console.log(filed_name.type);

});

problem with this is that radio type have same name but different id’s

<input type="radio" name="field_name5" id="field_name51" value="1">
<input type="radio" name="field_name5" id="field_name52" value="1" checked="checked">

and I endup with js error

Uncaught TypeError: Cannot read property ‘type’ of undefined

so what would be the best way to find out what the input type is before I can do

if(field.type ='radio'){

//set checked checked..
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:43:18+00:00Added an answer on June 13, 2026 at 9:43 am

    Take a look at this example. I just put it together very dirty, but you will get the idea 😉

    HTML:

    <form id="test">
        <input type="radio" name="abc" value="1"/>
        <input type="radio" name="abc" value="2"/>
        <input type="radio" name="abc" value="3"/>
        <input type="text" name="def" value="change" />
    </form>​
    

    JS:

    var values = {
        'abc': '2',
        'def': 'original'
    };
    var els = $('test').getElements('[name="abc"], input');
    els.each(function(el) {
        var defaultVal = values[el.get('name')],
            type = el.get('type');
    
        if (typeof defaultVal != 'undefined') {
            return;
        }
    
        if (type == "radio") {
            if (el.get('value') == defaultVal) {
                el.set('checked', 'checked');
            } else {
                el.erase('checked');
            }
        }
    
       if (type == "text") {
           el.set('value', defaultVal);
       }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that submits a search for blogs on my site via
i have a form that submits data to a database, i have a function
I have a form that the user submits and returns a result, but it
I've been working on creating a form that submits content into my database but
I have a form that submits data to a database in a CodeIgniter app
I'm developing a form that submits several user inputs to a database via post
I have a form that gets values from a database created by a model.
I have a form that submits a jQuery .ajax event every time menu's value
I want to submit a form that have many checkboxes with different values, using
I have a form that submits to a third-party service that runs some validation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.