14

I have a custom JS function that creates/inject a custom link into all elements in the page when it loads.

Before manipulation:

<div class="myimagediv">
       <img class="img-tag" src="#" data-src="alternative content I need" alt="">
    </div>

and now this custom function manipulates the element:

[].forEach.call(document.querySelectorAll('.myimagediv'), function(elem) {
            old_html = elem.innerHTML;

            new_html = '<a class="customlink" href="' + elem.querySelector('img').src + '">' + old_html + '</a>';
            elem.innerHTML = new_html;
        });

The newly manipulate element:

<div class="myimagediv">
      <a class="customlink" href="this should be the content of my data-src" title="">
          <img class="img-tag" src="#" data-src="alternative content I need" alt="">
      </a>    
    </div>

How can I get the data-src attribute from the IMG tag and inject it into my newly created custom link function?

I should use a var? and then call it, but I cannot grasp how to read the data-src and re-use it.

Any help would be very much appreciated.

5
  • 1
    var dataSrc = elem.querySelector('img').getAttribute('data-src'); Commented Sep 11, 2017 at 14:57
  • Try elem.querySelector('img').dataset.src Commented Sep 11, 2017 at 15:00
  • @Jared Smith please submit it as answer as that is exactly what I was looking for! Thanks! :-) Commented Sep 11, 2017 at 15:12
  • @user2513846 done. Commented Sep 11, 2017 at 15:17
  • @user2513846 - Take a look for my example - especially how to read multi word data attributes like data-something-else. Commented Sep 11, 2017 at 15:28

4 Answers 4

16

Just use the getAttribute method of the image element:

var dataSrc = elem.querySelector('img').getAttribute('data-src');
Sign up to request clarification or add additional context in comments.

Comments

5

Example - how to read data-* attrs in vanilla JS:

var elm = document.querySelector('any-tag')
var first = elm.dataset.whatever
var second = elm.dataset.somethingElse // camel case for multi-word

console.log(first, second)
<any-tag data-whatever="value1" data-something-else="value2" />

Comments

2

You just need to get it like any other attribute

<div id="test" data-myattr="toto">

</div>

alert(document.getElementById("test").getAttribute("data-myattr"));

JSFIDDLE

EDIT FROM @waldemarice:

HtmlElement contains the dataset property to get attribut prefixed by data-

    <div id="test" data-myattr="toto">

    </div>

  alert(document.getElementById("test").dataset.myattr);

JSFIDDLE

1 Comment

For data atrributes there is dataset. No need read it as any other attrs.
0

document.querySelectorAll() example:

document.querySelectorAll('.className').forEach(elem => console.log(elem.getAttribute('interesting-attribute')));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.