Solved

jquery for loop

Posted on 2014-04-29
9
202 Views
Last Modified: 2014-05-02
I need to have certain amount images show up where i is in the file name.
 
var maxRows = this.NoOfPages;  << here I have a total lets say max total is 6.

how can I add a for loop to add six images and using the [i] at the end of this.filename to appear. 1,2,3,4,5,6

Open in new window

 $("#stack").append($("<img>",
                            {
                                stack: "true",
                                src: "http://ing.com/" + this.FilePath + '/' + this.FileName[i],
                              Style: "width: 600px; height: 750px; z-index: 0; margin-left:auto; margin-right:auto; border: 5px solid rgb(255, 255, 255); box-shadow: 0px 0px 5px rgb(85, 85, 85)"
                            }));

Open in new window

0
Comment
Question by:sevensnake77
  • 5
  • 4
9 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40030672
Not sure I understand what you mean?

What are you trying to do with the append I am not familiar with that syntax?

According to the docs you can submit multiple parameters to append but they should ultimately all end up as DOM objects.

When you say you want to append the i to the filename - do you mean like this

filename.jpg => filename1.jpg

Or like this

filename.jpg => filename.jpg1

Or are your filenames formatted differently - if so can you give some examples.
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40032041
Well you are somewhat correct, I want to append the count but if the count is 6 then I want to have 6 images.  
The image names would be

filename1.jpg
filename2.jpg
filename3.jpg
filename4.jpg
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40032598
but if the count is 6 then I want to have 6 images
I understood that - just needed to know where you were expecting to append the count to filename or filename string - you answered that above.

Also need to know what you expect from this statemet
$("#stack").append($("<img>",
  {
    stack: "true",
    src: "http://ing.com/" + this.FilePath + '/' + this.FileName[i],
    Style: "width: 600px; height: 750px; z-index: 0; margin-left:auto; margin-right:auto; border: 5px solid rgb(255, 255, 255); box-shadow: 0px 0px 5px rgb(85, 85, 85)"
  }));

Open in new window

It does not look valid - I have not seen .append() used in this way before - can you elaboarate on what you are expecting the result from this to be.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 9

Author Comment

by:sevensnake77
ID: 40034529
$("#stack").append($("<img>",
  {
    stack: "true",
    src: "http://ing.com/" + this.FilePath + '/' + this.FileName[i],
    Style: "width: 600px; height: 750px; z-index: 0; margin-left:auto; margin-right:auto; border: 5px solid rgb(255, 255, 255); box-shadow: 0px 0px 5px rgb(85, 85, 85)"
  }));

Open in new window

The statement above will be
<img src="filename1" />
<img src="filename2" />
<img src="filename3" />
<img src="filename4" />
<img src="filename5" />
<img src="filename6" />

Open in new window

0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40035098
Still confused about this part
$("#stack").append($("<img>",
  {
    stack: "true",
    src: "http://ing.com/" + this.FilePath + '/' + this.FileName[i],
    Style: "width: 600px; height: 750px; z-index: 0; margin-left:auto; margin-right:auto; border: 5px solid rgb(255, 255, 255); box-shadow: 0px 0px 5px rgb(85, 85, 85)"
  }));

Open in new window

Do you have any references that documents the above usage of the .append() method. I have tried it in test code and it does not work.

Always interested to learn more - so if you can point me to where you found this code or where there is a description of this use of .append - that would be a good first step.
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40035365
how about lets keep it simple. create an array in jquery
example
var max = 6
I need to have array 1 2 3 4 5 6 in a loop or for each.
you have example of this.
0
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40036550
I know what you want to do just trying to understand how you want to do it.

Here is a simple example
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
var maxRows = 6;
var path = "path_to_file/";
var filename="image";
$(function() {
  for(i=1;i<=6;i++)
  {
    src = "http://ing.com/" + path + filename + i + '.jpg';
    $('#stack').append($('<img/>').attr('src', src));
  }
});
</script>
<style type="text/css">
</style>
</head>
<body>
<div id="stack"></div>
</body>
</html>

Open in new window

0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 40037678
thanks for your time and patience
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40037861
You are welcome - thanks for the points.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

828 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