1

I have data object that contains names. These names are taken from tr object and need to filled inside form element. Now data object has exact names as input element are in selected form.

Now I want to iterate through object and find input element with that name and fill its value.

Here is code

<form id="newItems">
        <input class="form-control"  name="Name" placeholder="Enter name" type="text">
 <input class="form-control"  name="Price" placeholder="Enter price" type="text">
</form>

//Data Object
    var data = {
       Name : "name",
       Price : 50
    };

for(var key in data){
   if (data.hasOwnProperty(key)) {
    console.log($('#'+modalId).find('input[name=key]'));return;
}

So far this return input element with name = key. But thats not what I need. I need whats inside key.

2
  • $('#'+modalId).find('input[name='+key+']').val() or $('#'+modalId).find('input[name='+key+']')[0].value Commented Aug 12, 2015 at 15:21
  • No that's not what I need. I will fill its value but first I need to check if that element is selected and its not. Commented Aug 12, 2015 at 15:23

1 Answer 1

2

You missed last } braces for loop. You put key as string it need to concadinate with + symbol like the below code.

 var data = {
       Name : "name",
       Price : 50
    };
var modalId ='hai';

for(var key in data){
   if (data.hasOwnProperty(key)) {
    console.log($('#'+modalId).find('input[name='+key+']'));
    console.log($('#'+modalId).find('textarea[name='+key+']'));
    console.log($('#'+modalId).find('select[name='+key+']'));
   }
}

Fiddle

Sign up to request clarification or add additional context in comments.

7 Comments

Yup that worked and I almost forgot to try such simple thing. But thanks for pointing out.
@Epistemologist - Glad it helps
One question though if I want to find form elements whether it is textarea or input or select how can I do that.
Need your html for this question
How can I get form element without mentioning its type. Whether it be input or textarea or whatever
|

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.