n00q
n00q

Reputation: 1

how to grab alt text to a function in javascript

what is the simplest way to grab the alt text to a function in JavaScript? tried .alt already. This is my current function:

I have tried .alt all day

function previewPic3(){ 
document.getElementById('image').style = "background: #fff url('URL')"; 
}

Upvotes: 0

Views: 152

Answers (1)

Scott Marcus
Scott Marcus

Reputation: 65845

You do indeed use the .alt property of the DOM object, but you may not have been successfully querying the DOM for the image in the first place.

function previewPic3(){ 
  console.log(document.querySelector('img').alt);
}

previewPic3();
<img src="https://example.com" alt="Hello!">

Upvotes: 2

Related Questions