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 ??????????????
});
asked Sep 26, 2012 at 20:03
Marco
2,7459 gold badges47 silver badges66 bronze badges
5 Answers 5
$("#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.
answered Sep 26, 2012 at 20:04
I Hate Lazy
49k13 gold badges89 silver badges79 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Fabrício Matté
.toArray()? Wouldn't the return value of .map be an array already?Shmiddty
I like your answer better. :)
I Hate Lazy
@FabrícioMatté: No, that would be either the native
Array.prototype.map, or $.map. This one returns a jQuery object. :-)Fabrício Matté
Oh yes my bad, I was thinking about
$.map. +1 =]$('.seourl').each(function(ele){
arr_str.push($(ele).val());
});
answered Sep 26, 2012 at 20:05
Shmiddty
13.9k1 gold badge38 silver badges52 bronze badges
Comments
$("#getFriendlyUrl").click(function() {
var arr_str = new Array();
$('.seourl').each(function() {
arr_str.push( $(this).val() );
})'
});
answered Sep 26, 2012 at 20:06
Sushanth --
55.8k9 gold badges70 silver badges109 bronze badges
Comments
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/
answered Sep 26, 2012 at 20:08
O'Mutt
1,5831 gold badge13 silver badges28 bronze badges
Comments
$("#getFriendlyUrl").click(function() {
var arr_str = new Array();
$('.seourl').each(function() {
arr_str.push($(this).attr('value'));
});
alert(arr_str);
});
answered Sep 26, 2012 at 20:09
Rahul
1,5793 gold badges18 silver badges37 bronze badges
Comments
lang-js