1

I need to form a string with the all values input fields within a div layer - using jquery

<div id="selection">
   <input class="field" id="1" type="hidden" value="A"/>
   <input class="field" id="2" type="hidden" value="B"/>
   <input class="field" id="3" type="hidden" value="C"/>
   <input class="field" id="4" type="hidden" value="D"/>
</div>

<input type="button" id="button" value="generate"/>


in this form:

id[1]=val[A]&id[2]=val[b]...so on


jquery:

 $(function() {
      $('#button').click(function() {
         //function goes here...


      });
    });
1
  • get the values back out using the class attribute in jQuery
    – Phil C
    Feb 23, 2011 at 10:37

4 Answers 4

1

If you use name instead of (or in addition to) id:

<input class="field" name="1" type="hidden" value="A"/>
<input class="field" name="2" type="hidden" value="B"/>
<input class="field" name="3" type="hidden" value="C"/>
<input class="field" name="4" type="hidden" value="D"/>

you can use serialize:

$('#button').click(function() {
   alert($('#selection input').serialize());
});

which gives you

1=A&2=B&3=C&4=D

If you really want to have the id[x] structure, you can give the elements the names id[1], id[2] etc.

Edit: Oh, somehow I overlooked that you want val[x] as well. This would not be possible with serialize, only if you really put val[x] as value in the fields. But why do you need such an obfuscated structure?


Btw. you are missing type="button" at your button.

1
  • Thank you very much, this is sufficient
    – Zebra
    Feb 23, 2011 at 11:03
1
<script>
    $(function() {
        $('#button').click(function() {

            var str = new Array();
            var count = 0;

            $('.field').each(
                function()
                {                   
                    str[count] = 'id['+$(this).attr('id')+']=val['+$(this).val()+']';
                    count++;
                }
            );
            alert(str.join('&'))
        });
    });
</script>
<div id="selection">
   <input class="field" id="1" type="hidden" value="A"/>
   <input class="field" id="2" type="hidden" value="B"/>
   <input class="field" id="3" type="hidden" value="C"/>
   <input class="field" id="4" type="hidden" value="D"/>
</div>
<input id="button" value="generate" type="button"/>
0
1

Another solution that gives the exact specified output and handles missing attributes gracefully:

See it in action at jsFiddle:

$(function() {
    $('#button').click(function() {

        var ResStr  = $('#selection input.field');

        ResStr  = ResStr.map (function () {
            var jThis   = $(this);
            var ID      = jThis.attr ("id");
            if (!ID)    ID  = "null";

            var VAL     = jThis.val ()
            if (!VAL)   VAL  = "null";

            return 'id[' + ID + ']=val[' + VAL + ']';

        } ).get () .join ('&');

        alert (ResStr);

    } );
} );
0

this returns all the html combined of all the inputs inside the div

var h = '';
var c = 0;
$('#selection input.field').each(function(){
 h += '&id['+(++c)+']=val['+$(this).val()+']';
});
h = h.slice(1);

alert(h);
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.