How to change part of HTML src elements with javascript - javascript

I want to change the word of the large crop link for word with javascript. The url changes every update, but the suffix "large" continues, but I wanna change every update to "crop". I am a beginner, I'm sorry for this boring question
<img id="cover" src="https://i1.sndcdn.com/artworks-000088629087-uoefpq-large.jpg">
to
<img id="cover" src="https://i1.sndcdn.com/artworks-000088629087-uoefpq-crop.jpg">

var elt = document.getElementById("cover");
elt.src = elt.src.replace("large", "crop");
Note that this may not work as you expect if the "crop" is in the url more than once.

Use document.getElementById to get the cover element
var src = document.getElemtnById('cover').src;
Modify the src variable
src = src.replace('large', 'crop');
Update the src attribute
document.getElemtnById('cover').src = src;

Related

I want to ask a question about javascript image src

<img src="../1.jpg" alt="" id="change-image">
<button id= "press-to-change">Press</button>
let count = 0;
let arr = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg"]
document.getElementById("press-to-change").addEventListener("click", function(){
count++;
document.getElementById("change-image").src = "../" + arr[count]
So we have an HTML with an image and a button and in JS we have an array with images and we want to change the src of the image when we press the button(this is only a part of the code)
The above code works fine but i have a question why with:
document.getElementById("change-image").src = "../" + arr[count] code works fine but with document.getElementById("change-image").src = `url('../${arrImage[count]}')` code doesn't work.
For example this next code from another project works perfectly imageContainer.style.backgroundImage = `url('../${arrImage[count]}')`
background-image is a for style of an element and uses css format.
src is for the actual source of the image element and requires a valid path only
With images, you use or assign to the src property only for <img> elements.
If you want to set the background image of an arbitrary (but non-<img>) element, you need to use different syntax: you have to assign to the style.backgroundImage property, and you have to surround the URL you're setting with url(...)
The rendered HTML markup looks like this:
<img src="foobar.png">
<div style="background-image: url('foobar.png');"></div>
They're not interchangeable. With the background-image property, you need to always use url(...). With the src attribute, you need to never use url(...).
The text: url(image.jpg) is syntax only used in CSS.
That’s why it works for backgroundImage but not src
An image will always need src="path/to/image.jpg" without url() surrounding it.

How to get a the url of an image from id in Javascript

I know it's such a beginner thing. So I have this image in a div with the id thumb.
<img id="thumb" src="https://url-to-a-image">
And this Javascript that it's a magnify script:
<script type="text/javascript">
var myImgSrc = document.getElementById("thumb").getElementsByTagName("img")
[0].src;
var evt = new Event(),
m = new Magnifier(evt);
m.attach({
thumb: '#thumb',
large: 'myImgSrc',
largeWrapper: 'preview'
});
</script>
As you can see I'm trying to get the image using myImgSrc and then I'm trying to use in the large: 'myImgSrc'. When I put the a fixed url in large: fixed-url-to-the-image, it works fine.
The element with #thumb id is the tag img it self, the current selector will not return the src value, so it should be simply:
var myImgSrc = document.getElementById("thumb").src;
You can get image src like this,
var thumb = document.getElementById("thumb").src;
You don't need to use getElementsByTagName.
let img = document.querySelector('#thumb');
console.log(img.src);
If you use img.src, you'll see the source of your img tag.
getElementsByTagName is superfluous - you already have the exact element you want - you selected it by its ID. You'd only need getElementsByTagName if you wanted to get one or more elements by their tag and work on them all, rather than identifying one precisely.
So actually the solution is very simple - just get the src attribute of the ID-selected element directly. Working demo:
var myImgSrc = document.getElementById("thumb").src;
console.log(myImgSrc);
<img id="thumb" src="https://url-to-a-image">

change some text in src of image tag using jquery [duplicate]

This question already has answers here:
Changing the image source using jQuery
(17 answers)
Closed 4 years ago.
I want to change one letter in the src of the image tag using js or jQuery.
<img src="/media/image_i/ball.png">
<img src="/media/image_i/bat.png">
I want to change that letter i in the src to a number.
Eg.it should look like this -
<img src="/media/image_4/ball.png">
<img src="/media/image_4/bat.png">
EDIT- When i used the solutions everything was working, but it was changing the "src" of all image tags to same as first image, so the second image which is "bat.png" is getting changed to "ball.png", so same image is displaying two times.
Thank you!
You can simply get the src attribute of the images, loop it over and replace the _i with _4 using JQuery. To check the below snippet works, you need to use inspect element of the browser and check the src attribute.
$(document).ready(function(){
$('img').each(function(){
var src = $(this).attr('src');;
$(this).attr('src', src.replace('_i','_4'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="/media/image_4/ball.png">
<img src="/media/image_4/bat.png">
You can do this job using regex.
$(document).ready(function(){
var src = $('img').attr('src');
var newsrc = src.replace(/_./g, '_4');
$('img').attr('src', newsrc);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="/media/image_i/ball.png">
You can develop it further.
For more : https://stackoverflow.com/a/2912904/5792209
First, you'll need to identify the particular img element you need to modify. This can be done in a variety of ways, like giving the img in question an id or a class that is unique. If the image is the only one using the src you've specified in your question, you can use that (and that's what I'm doing in the code that follows).
After getting the proper reference to the element, use the .attr() method to get/set the current src value and the standard String.replace method to swap the values:
var current = $("img[src='/media/image_i/ball.png']");
current.attr("src", current.attr("src").replace("_i", "_4"));
If this is all you need to do, JQuery is overkill. Standard JavaScript is just as simple:
var el= document.querySelector("img[src='/media/image_i/ball.png']");
el.src = el.src.replace("_i", "_4");

Change <img /> image with JavaScript Image object

I have a JavaScript Image object that I load dynamically with jQuery.
What I want to do is change a <img /> image with one stored on my Image object. How should I do this?
Note: I want to avoid changing the source of my <img /> tag as it downloads it again from the server, and I already have the image stored in my image object
You mean
$('#imageToChange').replaceWith(imageObject)
?
New Image object:
var Image_off = new Image();
Image_off.src = 'first.jpg';
image src change with jQuery:
$("#my_image").attr("src",Image_off.src);
With jQuery...
have both images already on your page and show or hide either one, based on a logical condition.
Make your new image in javascript memory, and then append it after the original image, then remove the original. You may also wish to cache the original before removing it in case you would like to re-use it.
html
<img id="replace" />
js
var img = new Image();
img.src = "someUri.png";
$("#replace").after(img);
$("#replace").remove();

random image want to change src

I want to load 3 random images from an array that contains the names of all of the images. I did this but the src doesn't work. I don't know why! Here is my code:
var images = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
var longitut = images.length;
var num =1+ Math.floor(Math.random()*longitut);
var foto="\"image"+num+".jpg\"";
foto=foto.toString();
alert(foto);
document.getElementById("imatge1").src=foto;
Try to remove quotes from the string value:
replace
var foto="\"image"+num+".jpg\"";
with
var foto="image"+num+".jpg";
Also, make sure that your image element has the proper id attribute value. In your case it should be:
<img id="imatge1"/>
The javascript code is correct, so it must be that you're either mistyping "image1" in the last line, or there's no element in your page.
Just add this to your HTML page:
<img src="" id="imatge1" />
You have a spelling mistake here,should be image1 not imatge1 should be:
document.getElementById("image1").src=foto;
Also did you look into relative and absolute paths when linking?, as it could be a path error.

Categories

Resources