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 7049693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:03:10+00:00 2026-05-28T03:03:10+00:00

The question has been asked multiple times, but this is not a repeat. I

  • 0

The question has been asked multiple times, but this is not a repeat. I am using a for loop to search through a div of elements including select, input etc. It matches the class name to an objects parameter and if it matches it sets the value of the element.

for(var i in obj) {
    if(obj.hasOwnProperty(i)) {
        $(editClone).find('.' +i).val(obj[i]);
    }
}

It works fine on input elements, but with the select element it removes all the options and simply sets the value on the actual element tag.

This is the select statement before I run my javascript:

<select class="type">
    <option value="Home">Home</option>
    <option value="Work">Work</option>
    <option value="Mobile">Mobile</option>
    <option value="Other">Other</option>
</select>

And after I run the code (inspected in firebug to verify):

<select class="type">Mobile</select>

Why does it do this? I thought val() was supposed to set a matching option tag to selected. The object value and option tags are matching, but it does not do what it is supposed to. What am I doing wrong here?

Turns out I had the DIV I was trying to clone nested in the wrong place in the HTML as well as a bad clone call in the javascript. Totally unrelated errors. The answer below is correct, it just didn’t work in my situation because I didn’t understand the problem I was having.

  • 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-05-28T03:03:11+00:00Added an answer on May 28, 2026 at 3:03 am
    <select class="type">Mobile</select>
    

    Is not valid. A select needs to have option tags inside of it


    Here’s a working version of your code:

    DEMO

    <select class="type">
        <option value="0">0</option>
        <option value="A">A</option>
    </select>
    

    Script:

    $(function() {
    
        var obj = { type: "A" };
    
        var editClone = $(document);
    
        for(var i in obj) {
            if(obj.hasOwnProperty(i)) {
                $(editClone).find('.' + i).val(obj[i]);
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this flavor of question has been asked multiple times, but I've spent
I know this question has been asked multiple times, but nobody has been able
I know this specific question has been asked before , but I am not
Sorry if this question has been asked before. On my search through SO I
I know this question has been asked here a few times before. But i
This question has been asked many times, but I tried every solution and none
I understand this question has been asked multiple times on various sites and forums
I know this question has been asked multiple times on this site in one
I know this question has been asked multiple number of times and i have
This question has been asked before ( link ) but I have slightly different

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.