How to change Element by tag name with javascript from iframe

1.3k views Asked by At

I am trying to change url(herf) from style . Check it

<iframe src="https://www.website.com/............" heght="100%" width="100%" >

#document

<!DOCTYPE html>

<html> 
<head></head>
<body>

<a class="class"  href="https://www.website.com/-----"  style="background: url(&quot;https://www.website.com/abc.png&quot;) center center / 100% no-repeat; width: 110px; height: 26px;"></a>

</body>
</html>

<iframe>

I want to change image url with javascript

: style="background: url(&quot;https://www.website.com/abc.png&quot;)

With Another url .

Like that

: style="background: url(&quot;https://www.website.com/xyz.png&quot;)

image xyz.png in important to change.

4

There are 4 answers

4
Luca Polito On

To change an element's style from Javascript, you can do so:

<!-- Suppose you have this <a> element -->
<a id="my-link" style="background: ...; ...">...</a>

<!-- This is the code -->
<script>
    document.getElementById("my-link").style.background = "url('https://www.website.com/abc.png') center center / 100% no-repeat";
</script>

Is this what you want?

3
Suneha Javid On
<script>
document.getElementById("iframe").setAttribute('style',' background-image: url(https://www.website.com/xyz.png);background-repeat: no-repeat;    background-size: 100% 100%;');
</script>

try this

3
Suneha Javid On
document.getElementsByTagName('iframe')[0].src = "https://www.website.com/xyz.png";

Get the iframe src and update the src. Hope this will help you

0
Muazam Aziz On

I have Find it , Try It

<!DOCTYPE html>
<html>
<body>

<ul>
  <li>Coffee</li>
  <li>Tea</li>
</ul>

<p>Click the button to change the text of the first list item (index 0).</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var list = document.getElementsByTagName("UL")[0];
    list.getElementsByTagName("LI")[0].innerHTML = "Milk";
}
</script>

</body>
</html>