looping problem

Hi
i have  a p tag with a title attribute that contain information i need. but there can be several p tags or even none, it is generated dynamically.

so i thought i need to push the needed content into an array and loop that array. but i cannot do it.

i attach the code i have tried and failed with.


best regards
this is my code:
 var paths = [];
                   $.each( function() { 
                     var thepath = $('p.msg').attr('title'); 
                      paths.push({
                          'p' : thepath
                      })
                    });
                    alert(pics);

and the generated html when it is generated:
<p  title="/assets/images/uploads/${name}" class="msg"></p>

Open in new window

LVL 1
derridaAsked:
Who is Participating?
 
Gurvinder Pal SinghConnect With a Mentor Commented:
From what i have understood from your post is, you are Okay with either way of solving this problem.

I would go with the approach of combining all into one and making one ajax call.

check this one
 var paths = "";
$('p.msg').each( function()
{
    paths += $(this).attr('name')  + ":" + $(this).attr('title') + ",";
     //you can replace name with id if name is not available
});

Now the paths variable has values like
image1:/assets/images/uploads/adam.jpg,image2:/assets/images/uploads/me.jpg,

so, at the server side you will have to split the string by comma "," first to get the pairs of name and value, then split each of them to extract name and values from it.
0
 
Gurvinder Pal SinghCommented:
try this code

var paths = [];
$('p.msg').each( function()
{
    var thepath = $(this).attr('title');
    paths[paths.length] = {
       'p' : thepath
    };
});
0
 
derridaAuthor Commented:
hi
i get:
[object Object],[object Object]
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
derridaAuthor Commented:
in this case i have generated 2 p tags
0
 
Gurvinder Pal SinghCommented:
<< hi
i get:
[object Object],[object Object]>>

It is because, you are pushing objects only!

if you only want the message then try

var paths = [];
$('p.msg').each( function()
{
    paths[paths.length] = $(this).attr('title');
});
0
 
derridaAuthor Commented:
hi now i get the right content but how can i get not all separated by a comme but one by one?

/assets/images/uploads/adam.jpg,/assets/images/uploads/me.jpg

 i need to get each path and send it into a function.
0
 
Gurvinder Pal SinghCommented:
just join them
http://www.w3schools.com/jsref/jsref_join.asp

alert(paths.join(","));
0
 
derridaAuthor Commented:
i get the same result. i need to split the result by the comme. i know how to do it in php but not in jquery.
0
 
Gurvinder Pal SinghCommented:
what result did you get when you executed this code?

alert(paths.join(","));
0
 
derridaAuthor Commented:
that:
/assets/images/uploads/adam.jpg,/assets/images/uploads/me.jpg

maybe i do not need to store them into array?

can i get each of the generated path without an array?
0
 
derridaAuthor Commented:
maybe i should explain what i am trying to do.

i have a form that uploads images (none or multiple). then in an ajax i neen to insert into the database the path of each image.
0
 
Gurvinder Pal SinghCommented:
if you want to split the result later, then use the split method of string
http://www.w3schools.com/jsref/jsref_split.asp

var strPath = paths.join(","); //to join them into array

var pathArray = strPath.split( "," ); //to get them back into array


but why do you want to first join and then split them into array. You can simply keep appending the string in the each() method itself

0
 
derridaAuthor Commented:
can you explain how to do your last suggestion?

i am not sure i really need an array here. as i said maybe i can jest get the title attribute for each image? i am not that comfortable in jquery yet.
0
 
derridaAuthor Commented:
maybe i need another loop over the array and then i`ll get each path?
0
 
derridaAuthor Commented:
hi
i did it like this:
   $.each(paths,function(key, value){
                     var path = value;
                     alert(path);
                 });

it does give me each path. is this the right way?
0
 
Gurvinder Pal SinghCommented:
var paths = "";
$('p.msg').each( function()
{
    paths += $(this).attr('title') + ",";
});

so now you have all the paths appended without using any array
0
 
derridaAuthor Commented:
well in this way each allert just add another path.

what do you think about the double loop i have? it does give me each one.
0
 
Gurvinder Pal SinghCommented:
for creating that loop, you need to first loop to create that array first.
Let me first confirm the requirement.
Do you want to store each image path separately against the name of each image or you want to store all path combined in one string?
0
 
derridaAuthor Commented:
i need each image path seperatly, and then send them into a database.
0
 
Gurvinder Pal SinghCommented:
then either you have to make a separate server side call for each image, or you have to combine them in one call in such a way that you can fetch all the paths separately against each image. Which approach you want to take?
0
 
derridaAuthor Commented:
you have to combine them in one call in such a way that you can fetch all the paths separately against each image.

i have no issue with the server side. i am quite new to make ajax forms.
this form has several regular form fields and a file fiels that can take multiple files.

when javascript is disabled i have no problem with the server side. but now i want to ajaxify the form. so when i make the ajax call i can send the regular form inputs and the images (if there is none or one or several).

hope i explained my self correctly.

best regards
0
 
derridaAuthor Commented:
thank you so much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.