I want to change the value of 'max-width' for all images, ideally just
by changing the _single_ CSS value in the <style> definition (if that
is possible). It shall be triggered by a click on a the table, say, by something (informally and obviously incorrectly written) like
<table onclick="document.querySelector('img').style.maxWidth='100px'">
How would such a Javascript command look like to work correctly?
Janis Papanagnou wrote:
I want to change the value of 'max-width' for all images, ideally just
by changing the _single_ CSS value in the <style> definition (if that
is possible). It shall be triggered by a click on a the table, say, by
something (informally and obviously incorrectly written) like
<table onclick="document.querySelector('img').style.maxWidth='100px'">
How would such a Javascript command look like to work correctly?
Your code is formally correct but does not do what you want. By
definition, querySelector() returns the first element that matches the selector given as the argument. So the code changes the width property
of the first img element.
To change them all, use the querySelectorAll() function, which returns
the list of all elements that match the selector. https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
You then need a little more code to iterate over that list, e.g.
<script>
function xpand() {
const images = document.querySelectorAll('img');
images.forEach((image) => {
image.style.maxWidth='100px'
})
}
</script>
<table onclick="xpand()">
So I also assume from that that we can't just change the single
CSS style attribute definition by Javascript to change all the
<img> elements,
and that this iteration over all <img>es is
unavoidable,
probably because the <style> elements are just
accessed once before building the DOM, and only in the DOM we
can change the attribute values? - Is that assumption correct?
Janis Papanagnou wrote:
So I also assume from that that we can't just change the single
CSS style attribute definition by Javascript to change all the
<img> elements,
We can. It's just not practical as a rule.
and that this iteration over all <img>es is
unavoidable,
Applying something to all img elements is essentially iterative anyway, though the iterative process can be hidden syntactically.
probably because the <style> elements are just
accessed once before building the DOM, and only in the DOM we
can change the attribute values? - Is that assumption correct?
It seems that changing the content of a <style> element with client-side JavaScript changes the rendering. I havenrCOt checked what the rCLHTML standardrCY says about it. Anyway, itrCOs clumsier. In JS, you can access
the <style> element as text, but there are no builtreAin tools for
operating on it structurally.
A very trivial example, based on the assumption that you just want to
replace the contents of the first <style> element with something else:
function chg() {
document.getElementsByTagName('style')[0].innerHTML =
'img { max-width: 100px; }';
}
On 11.01.2024 19:36, Jukka K. Korpela wrote:
Janis Papanagnou wrote:
So I also assume from that that we can't just change the single
CSS style attribute definition by Javascript to change all the
<img> elements,
We can. It's just not practical as a rule.
The motivation for my question was that I naturally use a CSS style definition to avoid specifying the specific attributes with every
single <img> individual attributes. My hope way that this simple
segregation of duties between CSS and HTML could be also reflected
in case of changes done through Javascript.
and that this iteration over all <img>es is
unavoidable,
Janis Papanagnou wrote:
On 11.01.2024 19:36, Jukka K. Korpela wrote:
Janis Papanagnou wrote:
So I also assume from that that we can't just change the single
CSS style attribute definition by Javascript to change all the
<img> elements,
We can. It's just not practical as a rule.
The motivation for my question was that I naturally use a CSS style
definition to avoid specifying the specific attributes with every
single <img> individual attributes. My hope way that this simple
segregation of duties between CSS and HTML could be also reflected
in case of changes done through Javascript.
and that this iteration over all <img>es is
unavoidable,
No not really. Here is one way:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Change the container</title>
<style>
.small img {
max-width: 50px;
}
.medium img {
max-width: 100px;
}
.large img {
max-width: 150px;
}
</style>
</head>
<body>
<table id="image_container" class="small">
<tr>
<td><img src="sample.jpg" alt="sample"></td>
</tr>
<tr>
<td><img src="sample.jpg" alt="sample"></td>
</tr>
<tr>
<td><img src="sample.jpg" alt="sample"></td>
</tr>
<tr>
<td><img src="sample.jpg" alt="sample"></td>
</tr>
<tr>
<td><img src="sample.jpg" alt="sample"></td>
</tr>
</table>
<script>
let state = 0;
const sizes = ['small', 'medium', 'large'];
const last = 3
const container = document.getElementById('image_container');
function resize() {
const pointer = (++state) % last;
container.className = sizes[pointer];
}
container.addEventListener('click', resize);
</script>
</body>
</html>
Take care,
Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
On 19.01.2024 17:37, Jonathan N. Little wrote:
No not really. Here is one way:
Thanks for this variant and for the code sample! - It's actually
how I hoped to be able to change all the sizes by changing one
property.
On 19.01.2024 17:37, Jonathan N. Little wrote:
Janis Papanagnou wrote:
On 11.01.2024 19:36, Jukka K. Korpela wrote:
Janis Papanagnou wrote:
So I also assume from that that we can't just change the single
CSS style attribute definition by Javascript to change all the
<img> elements,
We can. It's just not practical as a rule.
The motivation for my question was that I naturally use a CSS style
definition to avoid specifying the specific attributes with every
single <img> individual attributes. My hope way that this simple
segregation of duties between CSS and HTML could be also reflected
in case of changes done through Javascript.
and that this iteration over all <img>es is
unavoidable,
No not really. Here is one way:
Thanks for this variant and for the code sample! - It's actually
how I hoped to be able to change all the sizes by changing one
property.
I have two questions on that variant, a technical and a design
question...
First; initially I had problems incorporating that pattern into
my code. I got a null pointer error with this part:
const container = document.getElementById('image_container');
The reason was probably because I'm used to collect JS <script>
code at the beginning of the HTML files in the <head> section.
After relocating it to the end of the <body> it seemed to work.
Is this script code placement mandatory (to make that pattern
work) or is there some way to keep it at the top of the HTML
file?
Previously I had the getElementById() call within the function
so that it was non-null when called; the disadvantage was that
it's called with every click. (Not really an issue since it's
not time critical as it's called once and only during a slow
user interaction.)
And second; is there some kind of design convention what sort
of solution is preferable; iteration over all images, or change
of the class attribute (with one static getElementById()), or
change of the class attribute (with dynamic getElementById()?
I'm not sure whether there are such conventions; to me it seems
that there's so many different variants possible and used. But
sometimes there's some hidden advantages or caveats that I'm
not aware of (I have no deep insights in that matter), so I am
asking for insights and experiences.
[...]
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 00:51:47 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
10 files (20,373K bytes) |
| Messages: | 264,186 |