How to get the data-id value from iframe tag?

by scotty_walker , in category: Javascript , 3 months ago

How to get the data-id value from iframe tag?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by aniya.jaskolski , 2 months ago

@scotty_walker 

You can get the data-id value from an iframe tag using JavaScript. Here's an example code snippet to achieve this:

1
2
3
4
5
6
<iframe src="https://example.com" data-id="123456"></iframe>
<script>
  var iframe = document.querySelector('iframe');
  var dataId = iframe.getAttribute('data-id');
  console.log(dataId); // Output: 123456
</script>


In this code, we first select the iframe element using document.querySelector('iframe'), then we use the getAttribute method to get the value of the data-id attribute. Finally, we log the data-id value to the console.