views:

187

answers:

3

A few days ago I posted this question: http://stackoverflow.com/questions/1169387/switch-statement-and-loops-using-jquery-javascript
which ended up with me creating a series of divs using this code

  for (var i = 1; i <= $("#play option:selected").text(); ++i) {
    $("#play_"+i).slideDown().find("input").addClass("someClass");
  }

My problem is that I now need to get the value of each #play div and send that to a php script via ajax. If I had a set number of divs I could do that easily, but how do I do it when I don't know how many #play there will be?

FURTHER DESCRIPTION!

It seems I didn't explain myself clearly in my original question so I will try and explain things better.
I want to make an AJAX call to a remote php script using the $.post jQuery method. I can send information the remote script needs very easily. Here is an example:

     $("#submit").click(function() {
              $.post("../includes/process.php",
                     {
                       play_0: $("#play\_0").val(),
                       play_1: $("#play\_1").val(),
                       play_2: $("#play\_2").val(),
                       play_3: $("#play\_3").val(),
                       play_4: $("#play\_4").val()                                
                     },
                     function(data) {
                       $("#activityWindow").empty().append(data);       
              });
    });

The php script can now access this information through the $_POST array - just like a normal form submission.
If I have generated divs (#play_) using a loop, I can't hard code the $.post method in the way I have above. Really I need to include a loop in the syntax somewhere - I just can't work out how to do it! I hope this has made things clearer.

+3  A: 

jQuery has a attribute startsWith selector, then you can easily loop through them to create your object...

// Create an empty object
var data = {};

// Get all <div> elements that have an id attribute that starts with "play_"
var divs = $("div[id^=play_]");

// Loop through the <div> elements, using jQuery's each function
divs.each(function() {
  // Get the current div we are looping with jQuery
  var div = $(this);

  // Get the ID of the current div
  var id = div.attr("id");

  // Get the value of the current div
  var val = div.html();


  // Object properties can be set dynamically like this in Javascript
  data[id] = val;
});

// Loop is done, all properties have been set
alert(data.play_0);
Josh Stodola
why would you use innerHTML when jQuery has some very nice functions for handling that?
DLH
It's basically what html() would do if he used that:http://dev.jquery.com/browser/trunk/jquery/src/manipulation.js#L152
thedz
Exactly. There's really nothing "very nice" about the html function, but I edited my answer anyways.
Josh Stodola
I understand how to use this selector to access the elements, but I don't know how to combine it with making an ajax request. Ordinarily I would attach $.post to a function (eg a click) of a certain element and then send data using the syntax - something: $("#something").val() - this would then be accessed in php by $_POST['something']. How can I send an unknown number of elements?
musoNic80
More explanation, please. Makes no sense. What do you need to send?!
Josh Stodola
Sorry Josh - I've added more description above. Hope you can help!
musoNic80
I've updated my answer.
Josh Stodola
Thanks, Josh. Could you possibly talk me through your answer? My knowledge of Javascript obviously isn't up to understanding your solution. It would be great for me to understand it properly rather than just copy it. Thanks very much.
musoNic80
Comments added!
Josh Stodola
Thanks, Josh. That really helps. However, apologies for being slow, but how do I use this with the AJAX request I described above? Where should this loop be placed? What would be the correct syntax? Really appreciate your help.
musoNic80
Jesus Christ man, how about I just write the program for you? It's so obvious where you are passing your data in the post. Figure it out.
Josh Stodola
Hey, no need for that. I'm just starting out programming anything and the best way for me to learn is by more experienced programmers helping me out. You're the first person on this site or any other like it that has reacted like that. I'm really disappointed. If you don't want to help, don't bother writing anything at all.
musoNic80
A: 

Could you use the class in your selector like $("div.someClass").each(function() { This would only work if these are the only divs to use that class. If that's not the case you could just add another class to them.

Inside the function you could add the value to an array.

Edit: It would be better to use the "div[id^=play_]" selector. If you want to grab the value from the input and put it in an array you could say:

$("div[id^=play_] input").each(function() {
   valueArray.push($(this).val());
});
DLH
A: 

try this:

var x = $("[id^=play_]");
console.log(x.length);
Elzo Valugi