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
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>
ASKER
hi
i get:
[object Object],[object Object]
i get:
[object Object],[object Object]
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');
});
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');
});
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/ada m.jpg,/ass ets/images /uploads/m e.jpg
i need to get each path and send it into a function.
/assets/images/uploads/ada
i need to get each path and send it into a function.
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(","));
alert(paths.join(","));
ASKER
that:
/assets/images/uploads/ada m.jpg,/ass ets/images /uploads/m e.jpg
maybe i do not need to store them into array?
can i get each of the generated path without an array?
/assets/images/uploads/ada
maybe i do not need to store them into array?
can i get each of the generated path without an array?
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.
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
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
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.
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.
ASKER
maybe i need another loop over the array and then i`ll get each path?
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?
i did it like this:
$.each(paths,function(key,
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
$('p.msg').each( function()
{
paths += $(this).attr('title') + ",";
});
so now you have all the paths appended without using any array
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.
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?
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?
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you so much
var paths = [];
$('p.msg').each( function()
{
var thepath = $(this).attr('title');
paths[paths.length] = {
'p' : thepath
};
});