Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
How can I get the src and ID of an image in an array

I made a card as a "dynamic component"... My site can search any artist or song that is on Spotify. Each artist that you search appears in his own card.

I wrote the card, picture and title component in JS. Here is the way I did it:

for(let index = 0; index < artistIdResult.items.length; index++)
{
    var artistImage = document.createElement("input");
    artistImage.className = "artistImage";
    artistImage.type = "image";
    artistImage.src = artistIdResult.items[index].images[0].url;
    artistImage.id = artistIdResult.items[index].id;
    artistImage.width = 100;
    artistImage.height = 100;

    var artistText = document.createElement("h6");
    artistText.className = "artistText";
    var text = document.createTextNode(artistIdResult.items[index].name);
}

(The for just render the number of artist/songs).

This is how looks the card

When I saw the HTML on the inspector, appears the src, ID and more info from the image:

<input class="albumImage" type="image" src="https://i.scdn.co/image/ab67616d0000b273b13137d4579acad480ec9651" id="7m1DQbleCSsoBv1awh5qmu" width="80" height="80">

How can I get in an array the src and ID of each card where I pressed? I mean... I ask for the data in the JS, the HTML show the src and ID, but how can I get it in a variable and after in array?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

First we open a new array. You can then use jQuery click function to retrieve the values of attributes when clicked on it $(this). And push them to the array. You can check the array by clicking the test button. 🥳

jsonArray = [];

$('.albumImage').click(function() {
    getSrc = $(this).attr('src');
    getId = $(this).attr('id');
    jsonArray.push(getSrc);
    jsonArray.push(getId);
});
$('.test').click(function() {
    alert(jsonArray);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input class="albumImage" type="image" src="https://i.scdn.co/image/ab67616d0000b273b13137d4579acad480ec9651" id="7m1DQbleCSsoBv1awh5qmu" width="80" height="80">

<input class="albumImage" type="image" src="https://i.scdn.co/image/ab67616d0000b273b13137d4579acad480ec9651" id="1" width="80" height="80">

<button class="test">click to see array</button>

about 4 years ago · Juan Pablo Isaza Report

0

If I understand your requirement correctly, you want to access element attribute values in an array. If Yes, You can try this :

console.log(Array.from(document.getElementsByClassName('albumImage')).map(item => {
    return {
    'src': item.getAttribute('src'),
    'id': item.getAttribute('id')
  }
}));
<input class="albumImage" type="image" src="https://i.scdn.co/image1" id="1" width="80" height="80">
<input class="albumImage" type="image" src="https://i.scdn.co/image2" id="2" width="40" height="40">
<input class="albumImage" type="image" src="https://i.scdn.co/image3" id="3" width="20" height="20">

Update : Updating the answer as per the author comment, click event has been added to get the clicked image id and src attribute values.

Demo :

const artistIdResult = {
  items: [{
    images: [{
      url: 'https://i.scdn.co/image/ab67616d0000b273b13137d4579acad480ec9651'
    }],
    id: '1',
    name: 'alpha'
  }, {
    images: [{
      url: 'https://i.scdn.co/image/ab67616d0000b273b13137d4579acad480ec9651'
    }],
    id: '2',
    name: 'beta'
  }]
};

for(let index = 0; index < artistIdResult.items.length; index++)
{
  var artistImage = document.createElement("input");
  artistImage.className = "artistImage";
  artistImage.type = "image";
  artistImage.src = artistIdResult.items[index].images[0].url;
  artistImage.id = artistIdResult.items[index].id;
  artistImage.width = 100;
  artistImage.height = 100;

  var artistText = document.createElement("h3");
  artistText.className = "artistText";
  var text = document.createTextNode(artistIdResult.items[index].name);
  artistText.append(text);

  document.getElementById('app').append(artistImage)
  document.getElementById('app').append(artistText)

  var div = document.getElementById(artistIdResult.items[index].id);
  div.addEventListener('click', e => {
    console.log('id', e.target.id); // Id of clicked image
    console.log('src', e.target.src); // Src of clicked image
  });
}
<div id="app">
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Have a look at the jQuery click function.

$( "#target" ).click(function() {
  alert( "Handler for .click() called." );
});

This will attach a click handler on the element you're targeting. Inside the callback function you can then access the elements attributes using $(this).

From there you can push data into a global array or similar.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!