Skip to content
the poor coder

get img src and set as variable

Poor Man
Published
Reading time
2 min read
On this page

Getting img src and Setting it as a Variable#

Have you ever needed to get the source of an image and store it in a variable for later use? Maybe you want to display the same image in multiple places on your webpage without having to manually copy and paste the src every time. Here's how you can do it:

Method 1: Using JavaScript#

Text

// Get the image element by its ID
var img = document.getElementById("myImg");

// Get the src attribute of the image
var src = img.src;

// Set the src attribute of a new image element
var newImg = document.createElement("img");
newImg.src = src;

In this method, we first use JavaScript to get the image element by its ID. We then use the .src property to get the src attribute of the image element and store it in a variable called src. Finally, we create a new image element and set its src attribute to the src variable that we just created.

Method 2: Using jQuery#

Text

// Get the src attribute of the image
var src = $("#myImg").attr("src");

// Set the src attribute of a new image element
var newImg = $("").attr("src", src);

If you're using jQuery, you can accomplish the same thing with less code. In this method, we first use jQuery to get the src attribute of the image element and store it in a variable called src. We then use jQuery to create a new image element and set its src attribute to the src variable that we just created.

Method 3: Using PHP#

Text

<?php
// Get the src attribute of the image
$src = "img/myImg.jpg";
$src = htmlspecialchars($src); // Sanitize the input

// Set the src attribute of a new image element
echo "<img src='$src'>";
?>

If you're using PHP, you can use the htmlspecialchars() function to sanitize the input and prevent XSS attacks. In this method, we first store the src attribute of the image in a variable called $src. We then use PHP to echo out a new image element with the src attribute set to the $src variable that we just created.

Conclusion#

There are multiple ways to get the src attribute of an image and set it as a variable. Whether you're using JavaScript, jQuery, or PHP, these methods allow you to easily reuse images on your webpage without having to manually copy and paste the src every time.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC