473,671 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generate unique ID

I have a page with 641 images in it. Each image can be clicked with as
result that the image source will be changed. For this the images need
to have a unique ID. But to do this manually for 641 is just too much.
Is there a way to give each image his own unique id without setting
them myself?

The image:

<img src="images/hokje.gif" id=""
onClick="javasc ript:veranderKe uze(this.id);" alt="">

Jul 23 '05 #1
12 12290


ja********@gmai l.com wrote:

Is there a way to give each image his own unique id without setting
them myself?

<img src="images/hokje.gif" id=""
onClick="javasc ript:veranderKe uze(this.id);" alt="">


I don't think you need an id at all, simply pass the image object itself
to the function e.g.
<img onclick="verand erKeuze(this);"
and then
function veranderKeuze (img) {
img.src = 'whatever.gif';
}
Or what exactly do you need the id for in your function? You haven't
shown that.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
wrote:
I have a page with 641 images in it. Each image can be clicked with as
result that the image source will be changed. For this the images need
to have a unique ID. But to do this manually for 641 is just too much.
Is there a way to give each image his own unique id without setting
them myself?

The image:

<img src="images/hokje.gif" id=""
onClick="javasc ript:veranderKe uze(this.id);" alt="">

Putting 641 clickable images on a page sounds like a recipe for disaster,
but if you are determined to do that, why bother giving them id's at all?

Rewrite your veranderKeuze script to accept an image object instead of an
id and then just do:

<img src="images/hokje.gif" onClick="javasc ript:veranderKe uze(this);"
alt="">
Jul 23 '05 #3
ja********@gmai l.com wrote:
I have a page with 641 images in it. Each image can be clicked with as
result that the image source will be changed. For this the images need
to have a unique ID. But to do this manually for 641 is just too much.
Is there a way to give each image his own unique id without setting
them myself?

The image:

<img src="images/hokje.gif" id=""
onClick="javasc ript:veranderKe uze(this.id);" alt="">


It is possible to get all images in a document using
getElementsByTa gName and then iterating over them and seting a unique
id. However looking at your example I don't see why you need the id. How
about using the event in the 'veranderKeuze' function and getting the
target or srcElement to do something with the image?

PS
It is better to use onclick instead of onClick.
And remove the javascript: in the event.
Jul 23 '05 #4
Let me explain:

Each image will represent an field in a database.
When one image is clicked the image source will be changed and a update
request will be send to the database (probably a iframe location
refresh somewhere).
So i need the id to update the field which it belongs too.

Jul 23 '05 #5


JAPIO wrote:

Each image will represent an field in a database.
When one image is clicked the image source will be changed and a update
request will be send to the database (probably a iframe location
refresh somewhere).
So i need the id to update the field which it belongs too.


But if you have server side code and that stuff comes from a data base
then you should easily be able to generate the ids on the server, there
is certainly no need to to it "manually" as your first post said.
Of course you can loop through all images on the client and generate ids
there ( e.g.
var images = document.images , length = images.length;
for (var i = 0; i < length; i++) {
images[i].id = 'image' + i;
}
) but I don't see how that helps then to get the ids necessary to relate
to your data base fields, that is information you have available on the
server and consequently you should simply generate the needed ids there.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #6
This may be Microsoft only, but what about the "uniqueID" property.

http://msdn.microsoft.com/library/de...s/uniqueid.asp

Jul 23 '05 #7
JAPIO wrote:
Let me explain:

Each image will represent an field in a database.
When one image is clicked the image source will be changed and a update
request will be send to the database (probably a iframe location
refresh somewhere).
So i need the id to update the field which it belongs too.


Presumably your page is generated at the server, so why not generate
the id there? Any ID generated at the client will likely be
meaningless to the server, it will just the position in the images
array - and if JavaScript is not available, no IDs will be assigned.

You could add the database key as a query string on the end of the
image's href (again, server generated). But be aware users have a
habbit of spoofing this kind of stuff.

An ID must start with a letter, but can include numbers, hypens &
underscores (and other characters too) so you could just generate an
ID like 'id-x' or 'id_x' or 'id:x' where 'x' is just a sequential number.

--
Rob
Jul 23 '05 #8
JAPIO <ja********@gma il.com> wrote:
Let me explain:

Each image will represent an field in a database.
When one image is clicked the image source will be changed and a update
request will be send to the database (probably a iframe location
refresh somewhere).
So i need the id to update the field which it belongs too.


JAPIO,

Let's have the HTML like:

<img src="some_image .png"
<!-- class="clickabl e" if needed (see below) -->
width="W" height="H" alt="">

Then this JS (supposing that there is only one image to substitute for
all 641 images - that is a set of 641+1 images - otherwise, IMO, a set
of 2*641 images for one page is NOT reasonable):

<script type="text/javascript">

// stop event propagation
function stopBubble( e )
{
// DOM W3C
if( e.stopPropagati on )
{
e.stopPropagati on();
}
// DOM Microsoft
else if( window.event )
{
window.event.ca ncelBubble = true;
}
}

// perform some action on image src
function changeSrcClick( e )
{
// get event according to W3C or Microsoft DOM
e = ( e ) ? e : ( (window.event) ? window.event : null);

// if no event return
if ( !e ) return;

// stopBubble( e );

var node = ( e.target ) ? e.target : e.srcElement;

// bubbling bug workaround
if ( node.nodeType == 3 ) node = node.parentNode ;

// image substitution like this
node.src = substitutedImag e.src;

// some necessary action
...

}

function init()
{
// array of *all* images in document
IMGS = document.getEle mentsByTagName( "img" );

/* =============== = NOTE =============== =====

If you need to exclude some image(s) from this set of clickable images,
you can do whether, depending of your document structure:

- put clickable images in a div with id="clickable"
IMGS = document.getEle mentById("click able").getEleme ntsByTagName("i mg");

- give them a particular class like class="clickabl e"; in this case, in
next code, instead of direct method setting, just have a condition like:

if( IMGS[i].className == "clickable" )
{ IMGS[i].onclick = changeSrcClick; }

=============== =============== ============= */

for ( i=0; i < IMGS.length; i++ )
{
IMGS[i].onclick = changeSrcClick;
}
}
var IMGS = new Array();
var substitutedImag e = new Image( W, H );
substitutedImag e.src = "image_to_subst itute.png";

window.onload = init;

</script>

hih,

JJS.
--
Anti-spam : <http://public.xdi.org/=jj.solari>
Jul 23 '05 #9
Robert said the following on 7/22/2005 8:38 AM:
ja********@gmai l.com wrote:
I have a page with 641 images in it. Each image can be clicked with as
result that the image source will be changed. For this the images need
to have a unique ID. But to do this manually for 641 is just too much.
Is there a way to give each image his own unique id without setting
them myself?

The image:

<img src="images/hokje.gif" id=""
onClick="javasc ript:veranderKe uze(this.id);" alt="">

It is possible to get all images in a document using
getElementsByTa gName and then iterating over them and seting a unique
id.


It is even easier, and thus probably more efficient, to use the
document.images collection.
PS
It is better to use onclick instead of onClick.


Why? There is no difference.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
18590
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from tablename; Here this function generate unique id for each row of the table. Regards..
29
3732
by: Lauren Wilson | last post by:
Does anyone know how the following info is extracted from the user's computer by a Front Page form? HTTP User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0 I only ask because I believe I could use the same info as part of a scheme to generate a unique (or at least less common) serialized id code for the user's computer as part of a software locking and activation system. If I had a DLL...
2
4540
by: Chris Dunaway | last post by:
I have a web service which is accessed by a windows forms application. When the application submits a unit of work (a "job"), I want to return a job receipt or tracking number back to the application. My requirements are these: 1. The receipt number must be unique (like a guid is unique) 2. The receipt must be NON sequential. 3. If possible, I'd like it to be 10 to 12 characters long. 4. If possible, I'd like to include only...
11
4485
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any AutoNumber* fields in them. Correct me if I'm wrong, but I'm assuming this means that I cannot now alter these existing Access tables and change their primary key to an "AutoNumber" type. If I'm right about this, I need some suggestions as to the...
1
3647
by: Daniel Hilgarth | last post by:
Hello, I am currently trying to use XSLT for the creation of multiple HTML-files from a single XML-File. This HTML-files need to have links to each other. The following information might be important: There are some special nodes that will start a new HTML-page ("page-nodes"). Those nodes can be nested. Those nodes have an attribute "name" which is not necessarily unique. There are another special nodes that will create a link in one...
9
6450
by: Omatase | last post by:
I have a set of about 6 or so strings that I need to use to generate a unique hash. This hash will become the unique key in a database so the hash has to be the same each time I gen it for any 1 set of strings. Is there something out there that already does this written in javascript? I didn't find anything doing a google search.
9
18113
by: Robert Mago | last post by:
Is there a way to create a 10 characthers or less, alph-numeric string which is unique. I can't use the guid since its longer then 10 characthers. Also i cannot use a random number, since being random does not mean that its unique.
13
40566
by: gamernaveen | last post by:
I am coding a script , where basically the user has to enter his name , choose file , file comments if required and upload. The file comments , name , filename will be stored in the database with auto_increment id. The file gets uploaded to the server too. The problem is I want to generate a 10 digit unique code , which makes it simple for the user to download the file , he just has to enter the 10-digit code , and my script will deliver the...
15
37581
by: Ashish Khandelwal | last post by:
As MSDN is not giving us guarantee upon uniqueness of Hash Code, so could any one suggest me that how to generate a unique Hash Code for same string always, and generate different-2 Hash Code Different-2 string.
6
4346
by: =?Utf-8?B?QWxwaGFwYWdl?= | last post by:
Hello, I want to generate a unique ID for each page of my Asp.Net application. My first step is to generate a new Guid when the page is loaded for the first time and registers, stores this new Guid in a Dictionary which is saved in the ApplicationState, then I add a hidden field to the page __PageID, so I can get this unique ID on other Postbacks. No problem. The problem is about hackers. They can modify my hidden field to another...
0
8483
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2818
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.