in my little homemade picture gallery i find it difficult to accurately put the information about each picture into the correct place in the HTML doc.
each image is contained within a div, and i have dozens of them. each picture has data associated with it (title, finename, item #, caption, availability and cost), and each item has to go in the right place in the div. and i go bug-eyed trying to keep track of where i am while scrolling around.
it would be nice if i could set up a table or an array in my HTML doc itself, and just plop in the new info on a new row, and have a JavaScript or jQ routine grab the pieces of info and set them up in a new div.
after listening to crockford talk about security, i think i want to stay away from databases, and just stick to JS or jQ. but i don't know how to approach this problem.
i have a rough understanding of some javascript, and a beginners of jQ. and i like both of them. i guess in the back of my mind i was thinking along the lines of scripts that would grab a piece of data (from an array or table) and using something like innerHTML. paste it in the div.
it sounds to me like you need to set your data into objects (which is kind of js's version of a table). You can then access the object's properties using dot or square bracket notation.
If you are cycling through your images (like in a slideshow) it may be worth putting your objects into an array to make it easier to refer to them numerically.
although if you are changing the image onclick, you may end up with an object of objects, and you can use the name of the sub-object to retrieve the data:
Code:
<script>
var fotos={
dog:{title:"Fido",
filename:"Fido.jpg",
item:"3706",
caption: "woof!",
avail: "available"
},
cat:{title:"Whiskas",
filename:"Whiskas.jpg",
item:"9384",
caption: "meow!",
avail: "sold out"
}
}
var pic1="dog";
var pic2="cat"
alert(fotos[pic1].filename);
alert(fotos[pic2].caption);
</script>
That's the basics (or the easiest-to-maintain that I can see) - the finer points depend on how your page is set up and what you want to do
i have been learning about arrays since i read your excellent suggestions.
i am usually wrong when i make a guess, but i think an array will work for me best (it seems the simplest data input format, and data retrieval also).
i have started working it into my HTML doc. but before i get too far along, i will need to explore how to insert this tabulated data into a div, that is, in the correct places in the div.
my webpage is pretty simple. i have 3 columns and 5 rows of thumbnails on the left side of the page, each one is a button with an image (thumb) on it. when a thumb is clicked, a function displays a larger version of the thumb in a div on the right side of the page. no links. but i have to have as many div's for the big pictures as for the buttons. maybe that isn't the best way.
in writing this i realize that i have always been apprehensive about creating "objects", and maybe your first suggestion is even better. i like the text being next to the, field i guess. this is long winded so i will end it. any thoughts on placing object fields into div fields ?
you shouldn't be shy about object - javascript is full of them, and once you get the hang of them they make it very easy to access information, provided you structure them right.
From the sounds of it, you're trying to do something like the following:
first off, that was very kind of you. and i have begun to study it. i can immediately see it is better than mine. just so you don't think i have written in asking for you to do my coding for me, here is mine:
somehow my reply didn't get posted. i'll try again.
that is slick. very slick. i was able to easily add a new thumb and data and it still worked (usually my modifications break things).
i take it that within the variable foto, you made objects (dog group & cat group). each object contained within {}, and separated by a comma.
the other thing that i've never understood is "this". in your onClick functions:
onclick="changePic(this.id)" id="cat", does the "this.id" mean to use the id of the clicked element ?
it seems that you are saying to execute the function changePic, and go to the object in foto with the id "cat", and somehow "thepic" sorts it out. is "thepic" a parameter ?
Oh, it gets slicker than that. You're right in picking up on "this" - pretty much any time you have lots of functions doing basically the same thing you know that it can be all collapsed into one using the this keyword.
And what you can also do is iterate over your object and make the buttons dynamically, so that you never have to worry about your javascript and your html lining up. Just add another object to your "table" and the js does the rest.
just as you said, it made it simpler. i believe that i will be busy for about a week trying to actually understand what you did. but it is the kind of thing i like to do.
and once i understand it, boy is my website going to get simpler. in one days chat with you my HTML coding got about 50% smaller.
muchas gracias senor, your guidance is much appreciated.
i have done some reading about objects. but each time i think i am making a little progress, i look at your first coding example and say, but that is not what XEL did. and i remain stumped.
if i could ask a few fundamental questions re: your first code.
1- down in: onclick="changePic(this.id)" id="cat" on clicking, is the id referenced, or an object ?
2- up in: function changePic(thepic){ does "thepic" mean 'the referenced object' ?
3- regarding the getElementById : document.getElementById("tit").innerHTML=fotos[thepic].title;
am i right in that the first part document.getElementById("tit") means get the element with the id "tit" ?
and that the second part .innerHTML=fotos[thepic].title; means do this with it ?
ha, ha. No wonder you are getting stumped. Your questions don't have much to do with objects.
In this code:
Code:
onclick="changePic(this.id)" id="cat"
you are passing the string "cat" to the changePic function
Quote:
in: function changePic(thepic){ does "thepic" mean 'the referenced object' ?
not yet. All it's saying is that this function will accept one parameter, like a variable, hereby known as "thepic" which takes the value of whatever was passed to it (in your example, the string "cat")
once we get to this bit:
Code:
fotos[thepic].title;
you are saying: find the fotos object, and inside that, whatever object corresponds to the "thepic" variable (so, cat), and inside that, whatever property corresponds to 'title'
when you say "you are saying: find the fotos object, and inside that, whatever object corresponds" then i take it to mean that i don't have to use new before fotos to make it an object. and that the things in the curly braces separated by commas are objects too. everything is an object.
Everything is an object. The window is an object, arrays are objects, functions are objects. And yet javascript is not, by most counts, an object-oriented language. Go figure.
You can use new if you have a constructor function. I find them clumsy, but they have their uses, especially if you are making lots of objects.