2

Let's say I had something like any of the following:

<img src="image001.jpg"> <img src="../image001.jpg"> <img src="http://localhost/image001.jpg"> 

Using JavaScript (no jQuery and no CSS please), is it possible to style the image(s) in question, outside of the <img> tag.

For example if I have a website and I use Google Chrome Inspect>console, could I set a style on an image? Something like:

var images = document.querySelectorAll("img"); for(var i = 0;i < images.length;i++){ var image = images[i].src; if (image.indexOf('mystring') !== -1) { console.log(image); } 
4
  • You can live edit styles in the console. Beyond that it is really unclear what you are asking. Is this a site you control...or just a site you use? Commented Dec 17, 2016 at 21:53
  • 2
    What do you mean by "outside of the <img> tag"? Commented Dec 17, 2016 at 21:53
  • just - click on the "elements" panel - click on the image you want - from the right panel change the style as you want ! Commented Dec 17, 2016 at 21:55
  • Let's say we had a website that loaded 1000 images. All were hidden but I only wanted some to be shown, and I only knew their name... Commented Dec 17, 2016 at 21:57

4 Answers 4

3

Using JavaScript (not jQuery or CSS), is it possible to style the image(s) [...] ?

Yes, absolutely:

var images = document.getElementsByTagName('img'); images[0].style.backgroundColor = 'rgb(255,0,0)'; images[1].style.backgroundColor = 'rgb(255,255,0)'; images[2].style.backgroundColor = 'rgb(0,0,255)'; for (var i = 0; i < images.length; i++) { images[i].style.width = '100px'; images[i].style.height = '100px'; }
<img src="image001.jpg"> <img src="../image001.jpg"> <img src="http://localhost/image001.jpg">

Sign up to request clarification or add additional context in comments.

2 Comments

Very close - how would I target the filename specifically?
Just like you did with src, you can also use document.querySelectorAll('img[src*="image001.jpg"]')
1

You can use the src of each <img> element as a selector in CSS.

You can then style that specific image without changing anything in the html code.

Working example:

body { text-align: center } img { margin: 0 auto; } img[src="http://placehold.it/350x149"] { border: 10px solid black } img[src="http://placehold.it/350x150"] { border: 10px solid red } img[src="http://placehold.it/350x151"] { border: 10px solid blue }
<body> <img src="http://placehold.it/350x149"> <img src="http://placehold.it/350x150"> <img src="http://placehold.it/350x151"> </body>

2 Comments

That's using CSS which I asked not to use
My bad, I must've misunderstood the request as it sounded like you want either JavaScript or CSS but not jQuery
1

If I'm understanding you right..

If you want to establish an initial attribute on the image, you can specify a style for the image, or create a style to be applied to all your images. In this example, say we want a 2 pixel border with rounded corners.

Add a Class attribute like this to the image your html file:

<img src="http://localhost/image001.jpg" class="your-class"> 

"your-class" being the class you create-name it what you want.

Create the class either between style tags in your html file like this:

<style> .your-class { border: 2px; border-radius: 20px; } </style> 

or you can put the style in a separate CSS file (without the style tag) and reference it in the head tag of your html file like this:

<link rel="stylesheet" type="text/css" href="/css/styles.css"/> 

assuming your CSS is a file called "styles.css" and is located in a css folder contained in your site's root directory.

Edited: Not the answer the OP was looking for, but edited to include additional details for anyone landing upon this.

2 Comments

Welcome to Stack Overflow, I think your suggestion is what @pee2pee is looking for. You could go a little further in explaining how the page will use the css you have provided, either with in page styles or as an external css file. You can edit your post to include this and make your answer more complete
Sorry no, that's creating specific CSS and amending the original code. This could be for any website I visit in theory
0
var images = document.querySelectorAll("img"); for(var i = 0;i < images.length;i++){ var image = images[i]; var imageSrc = images[i].src; if (imageSrc.indexOf("a") !== -1) { console.log(image); images[i].style.width = '500px'; images[i].style.height = '500px'; images[i].style.zIndex = '9999999999999999999999999'; images[i].style.border = '20px solid red'; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.