最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to set a data-id in javascript? - Stack Overflow

programmeradmin1浏览0评论

I want to set data-id after the user put a URL in an input, already tried a lot of samples that I found here and none of these worked...

// im trying with this url
function getUrl() {
  var txt = document.getElementById('txtURL').value;
  var novaURL = txt.replace('/', '');
  document.getElementById('resultado').innerHTML = novaURL;
  imagem = document.getElementById('imagem').setAttribute('data-id', novaURL);
}
<script async src="//s.imgur/min/embed.js" charset="utf-8"></script>
<!-- ... -->
<input type="text" id="txtURL">
<input onclick="getUrl()" type="submit">
<h3 id="resultado">Resultado:</h3>
<blockquote id="imagem" class="imgur-embed-pub" lang="en" data-id="">
</blockquote>

I want to set data-id after the user put a URL in an input, already tried a lot of samples that I found here and none of these worked...

//https://imgur./SPrSZV7 im trying with this url
function getUrl() {
  var txt = document.getElementById('txtURL').value;
  var novaURL = txt.replace('https://imgur./', '');
  document.getElementById('resultado').innerHTML = novaURL;
  imagem = document.getElementById('imagem').setAttribute('data-id', novaURL);
}
<script async src="//s.imgur./min/embed.js" charset="utf-8"></script>
<!-- ... -->
<input type="text" id="txtURL">
<input onclick="getUrl()" type="submit">
<h3 id="resultado">Resultado:</h3>
<blockquote id="imagem" class="imgur-embed-pub" lang="en" data-id="">
</blockquote>

the data-id should be SPrSZV7, I got in the console the error Cannot read property 'setAttribute' of null, why is this null?

Share Improve this question edited May 5, 2019 at 11:03 Nazim Kerimbekov 4,7819 gold badges36 silver badges58 bronze badges asked May 2, 2019 at 16:34 IgorIgor 4086 silver badges18 bronze badges 3
  • did you pass your JS code before body code ? – Mister Jojo Commented May 2, 2019 at 17:02
  • no, my js code is above the body – Igor Commented May 2, 2019 at 17:21
  • so that's why document.getElementById ('imagem') is null – Mister Jojo Commented May 2, 2019 at 17:25
Add a ment  | 

3 Answers 3

Reset to default 3

What's happening is the imgur embed library is loading and overwriting your blockquote with an iframe, so your ID of "imagem" disappears when the iframe loads in its place. Trying wrapping the blockquote with a div container that has data-id attribute, and setting the data value there for reference. Or you can try and reference the imgur iframe ID that appears when embed happens: 'imgur-embed-iframe-pub-' instead of using a div wrapper. Please see the following example:

//https://imgur./SPrSZV7 im trying with this url
function getUrl() {
  var txt = document.getElementById('txtURL').value;
  var novaURL = txt.replace('https://imgur./', '');
  document.getElementById('resultado').innerHTML = novaURL;
  var imagem = document.getElementById('container-for-imgur');
  imagem.dataset.id = novaURL;
  console.log(imagem.dataset.id);
}
<script async src="//s.imgur./min/embed.js" charset="utf-8"></script>

<input type="text" id="txtURL" value="https://imgur./SPrSZV7" />
<input onclick="getUrl()" type="submit" />
<h3 id="resultado">Resultado:</h3>
<div id="container-for-imgur" data-id="">
  <blockquote id="imagem" class="imgur-embed-pub" lang="en" data-id="">
  </blockquote>
</div>

why is this null?

because you pass your JS code before body code ?


use dataset => https://developer.mozilla/en-US/docs/Web/API/HTMLElement/dataset

document.getElementById('imagem').dataset.id = novaURL;

remark : there is no return value so do not use imagem =

Your code works. You can see in inspect that the data attribute is correctly set up. You must have been experiencing a script load problem in which the content DOM is not loaded before the script execution.

    //https://imgur./SPrSZV7 im trying with this url
    function getUrl() {
        var txt = document.getElementById('txtURL').value;
        var novaURL = txt.replace('https://imgur./', '');
        document.getElementById('resultado').innerHTML = novaURL;
        imagem = document.getElementById('imagem').setAttribute('data-id', novaURL);
    }
<input type="text" id="txtURL">
    <input onclick="getUrl()" type="submit">
    <h3 id="resultado">Resultado:</h3>
    <blockquote id="imagem" class="imgur-embed-pub" lang="en" data-id="">
    </blockquote>

发布评论

评论列表(0)

  1. 暂无评论