mrbean Posted August 10, 2012 Share Posted August 10, 2012 I have made an loop for images to make them an javascript image object. Just like image[1] image[2] They al have assigned .src attribute. But the thing is I don't know which image what is so I want to refer to the image name. I want to assign a name for every image in the loop. Just like I have a list of names ordered by numbers so the value of imgname[1] must be commented out in image[1] So i can refference by the comment. I hope you understand what I am trying to say. And sorry for my bad english. Quote Link to comment https://forums.phpfreaks.com/topic/266932-image-note/ Share on other sites More sharing options...
webster08 Posted August 11, 2012 Share Posted August 11, 2012 I really don't have a clue, as to what your looking for; but since no one else responded to you and because I am bored, lol... I am going to try to give it a shot. Try something like this; maybe it will get you started: <script> var created, created2 = "no"; function getImage(imgName) { var image1 = new Image(); image1.setAttribute("id","Photo 1"); image1.setAttribute("src","pic1.jpg"); image1.setAttribute("width","1"); image1.setAttribute("height","1"); var image2 = new Image(); image2.setAttribute("id","Photo 2"); image2.setAttribute("src","pic2.jpg"); image2.setAttribute("width","1"); image2.setAttribute("height","1"); // validation if (imgName == "Photo 1") { // insert image only once into page if (created != "yes") { document.body.appendChild(image1); created = "yes"; } var imgSRC = document.getElementById(""+imgName+"").src; alert(imgSRC); // for example purposes } else if (imgName == "Photo 2") { // insert image only once into page if (created2 != "yes") { document.body.appendChild(image2); created2 = "yes"; } var imgSRC = document.getElementById(""+imgName+"").src; alert(imgSRC); // for example purposes } } </script> <button onclick="getImage('Photo 1')">See The SRC For "Photo 1"</button> <button onclick="getImage('Photo 2')">See The SRC For "Photo 2"</button> If you play around with it some; I am sure you can cut down on some of the code recursion, but maybe this will help you out... at least for demo purposes. If you have some code to post; that will probably help people to understand what you trying to do and will, in turn... get more responses to your question. Quote Link to comment https://forums.phpfreaks.com/topic/266932-image-note/#findComment-1368528 Share on other sites More sharing options...
mrbean Posted August 11, 2012 Author Share Posted August 11, 2012 -snap- Thanks for your reply, but let me explain this again with some code examples and now more properly. var image = new Array(); var i = 0; while (i<5) { image[i] = new Image(); image[i].src = "test0"+i+".png"; i++; } // So now I have a list of images. // What if I want to choose the image with source: test01.png // but by reffering an assigned name. an example from mysql syntax: Select image.src from image where image.something = happysmiley. Like I have an list of images but one has something where I can refer to to find that specific image. Reffering by name of the image for example. Quote Link to comment https://forums.phpfreaks.com/topic/266932-image-note/#findComment-1368583 Share on other sites More sharing options...
Christian F. Posted August 11, 2012 Share Posted August 11, 2012 If you know that you want image 01, and you have created the array with image, then you already know the index you want: Image01 == index 1. Quote Link to comment https://forums.phpfreaks.com/topic/266932-image-note/#findComment-1368587 Share on other sites More sharing options...
mrbean Posted August 11, 2012 Author Share Posted August 11, 2012 If you know that you want image 01, and you have created the array with image, then you already know the index you want: Image01 == index 1. I want to refer by an assigned name to it, not counting to finally find the image and say i want image[1323] or something like that, it wil cost alot of time. Quote Link to comment https://forums.phpfreaks.com/topic/266932-image-note/#findComment-1368588 Share on other sites More sharing options...
Adam Posted August 11, 2012 Share Posted August 11, 2012 What you're asking to do doesn't make any sense. Why would you want to refer to the image by it's src of test01.png, when you know you can access it through image[1]? Sounds like you're trying to over complicate things. If in your actual code you're not using numeric values like that though, you could just use a standard object instead: function createImage(src) { var image = new Image(); image.src = src; return image; } var images = { '/path/to/foo.png': createImage('/path/to/foo.png'), '/path/to/bar.png': createImage('/path/to/bar.png'), '/path/to/baz.png': createImage('/path/to/baz.png') } console.log(images); console.log(images['/path/to/foo.png']); console.log(images['/path/to/bar.png']); console.log(images['/path/to/baz.png']); http://jsfiddle.net/UQHey/ If that is the case though you should have posted your code, or mentioned you're not using sequential naming like that. Quote Link to comment https://forums.phpfreaks.com/topic/266932-image-note/#findComment-1368602 Share on other sites More sharing options...
mrbean Posted August 11, 2012 Author Share Posted August 11, 2012 What you're asking to do doesn't make any sense. Why would you want to refer to the image by it's src of test01.png, when you know you can access it through image[1]? Sounds like you're trying to over complicate things. I am not asking that, but it's already fixed Quote Link to comment https://forums.phpfreaks.com/topic/266932-image-note/#findComment-1368616 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.