Link to home
Start Free TrialLog in
Avatar of derrida
derrida

asked on

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

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

try this code

var paths = [];
$('p.msg').each( function()
{
    var thepath = $(this).attr('title');
    paths[paths.length] = {
       'p' : thepath
    };
});
Avatar of derrida
derrida

ASKER

hi
i get:
[object Object],[object Object]
Avatar of derrida

ASKER

in this case i have generated 2 p tags
<< 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');
});
Avatar of derrida

ASKER

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.
Avatar of derrida

ASKER

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.
what result did you get when you executed this code?

alert(paths.join(","));
Avatar of derrida

ASKER

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?
Avatar of derrida

ASKER

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

Avatar of derrida

ASKER

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.
Avatar of derrida

ASKER

maybe i need another loop over the array and then i`ll get each path?
Avatar of derrida

ASKER

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?
var paths = "";
$('p.msg').each( function()
{
    paths += $(this).attr('title') + ",";
});

so now you have all the paths appended without using any array
Avatar of derrida

ASKER

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.
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?
Avatar of derrida

ASKER

i need each image path seperatly, and then send them into a database.
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?
Avatar of derrida

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of derrida

ASKER

thank you so much