Solved

looping problem

Posted on 2011-09-04
22
294 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:gurvinder372
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
 
LVL 40

Expert Comment

by:gurvinder372
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:gurvinder372
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:gurvinder372
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:gurvinder372
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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:gurvinder372
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:gurvinder372
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:gurvinder372
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:
gurvinder372 earned 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now