Marco
Marco

Reputation: 2737

populate array with multiple input fields

I want to use jQuery to populate an array with values from input fields with a class of 'seourl'...

<input id="title" class="seourl" name="title" type="text" value="???">
<input id="subtitle" class="seourl" name="title" type="text" value="???">
<input id="subtitle2" class="seourl" name="title" type="text" value="???">

<a id="getFriendlyUrl" href="">get url friendly</a>

How do i populate the array with input fields of class 'seourl'?

$("#getFriendlyUrl").click(function() {

    var arr_str = new Array();

    ?????? POPULATE ARRAY with input fields of class 'seourl', how ??????????????

});

Upvotes: 0

Views: 1988

Answers (5)

Rahul
Rahul

Reputation: 1579

$("#getFriendlyUrl").click(function() {

    var arr_str = new Array();
    $('.seourl').each(function() {
        arr_str.push($(this).attr('value'));

    });
    alert(arr_str);
});

Upvotes: 0

O&#39;Mutt
O&#39;Mutt

Reputation: 1592

html:

<input id="title" class="seourl" name="title" type="text" value="???">
<input id="subtitle" class="seourl" name="title" type="text" value="???">
<input id="subtitle2" class="seourl" name="title" type="text" value="???">

<a id="getFriendlyUrl" href="">get url friendly</a>​

JS w/jquery:

$("#getFriendlyUrl").click(function() {

    var arr_str = new Array();

    $(".seourl").each(function(index, el) {
     arr_str[index] = $(el).val();   
    });
    alert(arr_str[0] + arr_str[1] + arr_str[2]);

});​

jsfiddle: http://jsfiddle.net/Mutmatt/NmS7Y/5/

Upvotes: 0

Sushanth --
Sushanth --

Reputation: 55740

$("#getFriendlyUrl").click(function() {
    var arr_str = new Array();

    $('.seourl').each(function() {

        arr_str.push( $(this).val() );
    })'

});

Upvotes: 0

Shmiddty
Shmiddty

Reputation: 13967

$('.seourl').each(function(ele){
    arr_str.push($(ele).val());
});

Upvotes: 0

I Hate Lazy
I Hate Lazy

Reputation: 48761

$("#getFriendlyUrl").click(function() {

    var arr_str = $('.seourl').map(function() {
                                       return this.value;
                                   }).toArray();
});

You can use jQuery to get the .value if you want.

return $(this).val();

Either way, you'll end up with an Array of the values.

Upvotes: 7

Related Questions