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

180
Views
Javascript grabbbing a html5 data- value to use as a var

So I want to customize the src via js with hidden data but just can't figure out the right flow to write it. Looking to grab html5 data- to change the src. Any time I have tried to write it can't get it to grab my data- with Var.

referencing question 19320436 for var from data when looking for answers

tried

<html>
<body>

<p id="test" class="customClass" data-newsrc="https://stackoverflow.com"></p>

<script>
var value = $('.customClass').data('newsrc');

document.getElementById("test").innerHTML= '<iframe id="myframe" src=""\>\</iframe>';

document.getElementById("myframe").src= value;
</script>
</body>
</html>

works but is not grabbing a new value from page

<html>
<body>

<p id="test" class="customClass" data-newsrc="https://stackoverflow.com"></p>

<script>

var value = "https://stackoverflow.com/qestions"

document.getElementById("test").innerHTML= '<iframe id="myframe" src=""></iframe>';

document.getElementById("myframe").src= value;
</script>
</body>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First select the #test element, you'll need it more than once. Without using jQuery you can select the data-newsrc property by accessing it from the dataset property.

Instead of writing your HTML in a string, create a new element object with document.createElement, this saves you the step of having to reselect the iframe because you already have a reference.

After setting the src, append the iframe to the test element.

const test = document.getElementById('test');
const src = test.dataset.newsrc;
const iframe = document.createElement('iframe');
iframe.src = src;
test.append(iframe);
<p id="test" class="customClass" data-newsrc="https://stackoverflow.com"></p>

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!