?
Solved

looping problem

Posted on 2011-09-04
22
Medium Priority
?
305 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:derrida
  • 13
  • 9
22 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36482470
try this code

var paths = [];
$('p.msg').each( function()
{
    var thepath = $(this).attr('title');
    paths[paths.length] = {
       'p' : thepath
    };
});
0
 
LVL 1

Author Comment

by:derrida
ID: 36482478
hi
i get:
[object Object],[object Object]
0
 
LVL 1

Author Comment

by:derrida
ID: 36482481
in this case i have generated 2 p tags
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36482489
<< 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
 
LVL 1

Author Comment

by:derrida
ID: 36482501
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36482510
just join them
http://www.w3schools.com/jsref/jsref_join.asp

alert(paths.join(","));
0
 
LVL 1

Author Comment

by:derrida
ID: 36482512
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36482517
what result did you get when you executed this code?

alert(paths.join(","));
0
 
LVL 1

Author Comment

by:derrida
ID: 36482539
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
 
LVL 1

Author Comment

by:derrida
ID: 36482545
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36482553
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
 
LVL 1

Author Comment

by:derrida
ID: 36482557
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
 
LVL 1

Author Comment

by:derrida
ID: 36482560
maybe i need another loop over the array and then i`ll get each path?
0
 
LVL 1

Author Comment

by:derrida
ID: 36482571
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36482577
var paths = "";
$('p.msg').each( function()
{
    paths += $(this).attr('title') + ",";
});

so now you have all the paths appended without using any array
0
 
LVL 1

Author Comment

by:derrida
ID: 36482586
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36482606
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
 
LVL 1

Author Comment

by:derrida
ID: 36482614
i need each image path seperatly, and then send them into a database.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36482671
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
 
LVL 1

Author Comment

by:derrida
ID: 36482716
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
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 36482894
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
 
LVL 1

Author Closing Comment

by:derrida
ID: 36483147
thank you so much
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question