views:

31

answers:

2

So I have a few inputs with the name session and they increment.... session0 session1 session2. If I want to run them through a loop how would I do that. I'm using jquery.

for example..

for(i=0,i<10,i++)
{
var num = (session + i).value + i;
}

So what I want is the loop to go through all the inputs with prefix session and output their values to a variable. This out put could go into a array. if array obviously would look more like.

num[i] = stuff+i;

thanks.

Okay well I ended up going with the JavaScript. Thanks for all the help it works great. here was the final code.

function get()
{
var alldata;
var values = [];
      $("input[name^='media']").each(function(i, anInput) {
        var check = values[i] = $(anInput).attr('checked');
        if(check == true)
        {
        var ID = "session" + i;
        var type = $(anInput).attr('value');
        if(i > 9)
        {
        var cont = "#ex"+ i;
        }
        else{
        var cont = "select[name='" + ID + "']";
        }
        var scope = $(cont).attr('value');
        if(!alldata)
        {
        alldata = type + ': ' + scope + ' ';
        }
        else
        alldata =  alldata + ' ' + type + ': ' +  scope + ' ';
        };

      })
$('#sessions').attr('value',alldata);
}
+3  A: 

Use the starts with selector ^ and each function like this:

var arr = new array();
$('input[name^="session"]').each(function(){
   alert($(this).val());
   // arr[] = $(this).val();
});
Sarfraz
cool thanks I'll try this. I've never seen it like this before.
creocare
Slight variation, using map to get everything into an array. `arr = $('input[name^="session"]').map(function(){ return this.value; });`
Patrick McElhaney
@Patrick McElhaney: Yup that's more easier/better. Thanks :)
Sarfraz
I think part of why you don't see this type of thing more often is most devs would just assign the same class to all these inputs and use the jQuery selector to find all elements with that class assignment. The logic works the exact same, but if you are likely to style all these inputs the same way anyways..the class selector could be cleaner..
fdfrye
oops this is great but how would you do this in php.....
creocare
+2  A: 

How about this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>
    <head>
      <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
      <script type="text/javascript" charset="utf-8">
        $(document).ready(function() {
          var values = [];
          $("input[name^='session']").each(function(i, anInput) {
            values[i] = $(anInput).val();
          })
        })
      </script>
    </head>
    <body>
        <input type="text" value="5" name="session0" />
        <input type="text" value="7" name="session1" />
        <input type="text" value="2" name="session2" />
        <input type="text" value="1" name="session3" />
    </body>
</html>
Jared Hales
i was too slow :)
Jared Hales
But you went to the trouble of making a complete, working example. Have an upvote. :-)
Patrick McElhaney
ha, thanks! i don't use this site a lot, but when i do, i try to offer the most "complete" example i can whip up in a few minutes.
Jared Hales