0

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 ??????????????

});

5 Answers 5

7
$("#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.

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

4 Comments

.toArray()? Wouldn't the return value of .map be an array already?
I like your answer better. :)
@FabrícioMatté: No, that would be either the native Array.prototype.map, or $.map. This one returns a jQuery object. :-)
Oh yes my bad, I was thinking about $.map. +1 =]
0
$('.seourl').each(function(ele){
    arr_str.push($(ele).val());
});

Comments

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

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

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

});

Comments

0

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/

Comments

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

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

    });
    alert(arr_str);
});

Comments

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.