Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #19389
| Message-ID | <3001426.0veXviQ4o9@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-04-21 00:22 +0200 |
| Subject | Re: Based on its src value, find an image and assign an ID? |
| Newsgroups | comp.lang.javascript |
| References | <kkv0sq$8mp$1@news.albasani.net> <kkv24n$b15$1@news.albasani.net> |
Stefan Weiss wrote:
> On 2013-04-20 23:21, Tuxedo wrote:
>> Using a same header image, e.g. <img src=placeholder.gif> that is
>> generated and cannot be changed after it's been initially loaded into a
>> page view, can an ID be assigned later in the document flow?
>
> The easiest way is to use querySelector() or querySelectorAll():
>
> var ele = document.querySelector("[src='placeholder.gif']");
>
> If support for older browsers is a concern, there are some libraries
> that can simulate the querySelector* methods (usually returning a static
> array instead of a live collection).
>
> jQuery has something like that built-in (called Sizzle, I believe), as
> do most other general purpose libraries, and there is one popular
> library only for this purpose, but its name escapes me at the moment.
>
> Other than that, you could try XPath (where supported), or just iterate
> over document.images and compare the src-attributes until you find your
> image.
>
> If you don't actually need the element (for example, if you only want to
> hide it), you can also do that with CSS:
>
> img[src='placeholder.gif'] { display: none }
A correct answer that actually relates to the question is: Once you have a
reference to the image object, say “img”, execute:
img.id = "foo";
where "foo" is an example new ID.
The ID should not be assigned before the “DOMContentReady” or “load” event
of the document occurs, so the suggested “onload” event-handler attribute
value is a suitable *base* execution context.
IDs must be unique in the element's home subtree in HTML5; in HTML 4.01 they
must be unique in the whole document. They must also follow certain naming
rules (see the Specifications for details). Although it is trivial to
generate such an ID after the fact, usually it is better to give elements
IDs before they are parsed.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Based on its src value, find an image and assign an ID? Tuxedo <tuxedo@mailinator.com> - 2013-04-20 23:21 +0200
Re: Based on its src value, find an image and assign an ID? Stefan Weiss <krewecherl@gmail.com> - 2013-04-20 23:42 +0200
Re: Based on its src value, find an image and assign an ID? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-21 00:22 +0200
Re: Based on its src value, find an image and assign an ID? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-21 00:36 +0200
Re: Based on its src value, find an image and assign an ID? Stefan Weiss <krewecherl@gmail.com> - 2013-04-21 00:52 +0200
Re: Based on its src value, find an image and assign an ID? Scott Sauyet <scott.sauyet@gmail.com> - 2013-04-20 17:20 -0700
Re: Based on its src value, find an image and assign an ID? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-21 11:09 +0200
Re: Based on its src value, find an image and assign an ID? Scott Sauyet <scott.sauyet@gmail.com> - 2013-04-23 06:32 -0700
Re: Based on its src value, find an image and assign an ID? x@x.com (znôrt) - 2013-04-21 00:56 +0200
Re: Based on its src value, find an image and assign an ID? x@x.com (znôrt) - 2013-04-21 00:59 +0200
csiph-web