2

I got an assignment to build a website and I was wondering, how can I change a iframe on clicking a link without using javascript or giving the iframe a ID? I'd rather do it with a ID aswell, but yeah, that's not allowed.

Brino
2,5121 gold badge23 silver badges37 bronze badges
asked Nov 16, 2015 at 17:34
4
  • no you can't modify the <iframe> tag itself without JS, especially by clicking something inside the iframe. but once you get past that hurdle, you can target the iframe any way you want. even if there's no ID on the specific tag, you can target any tag in a DOM tree by its tree "address", tag name, etc... Commented Nov 16, 2015 at 17:36
  • 1
    No need for JavaScript at all, see stackoverflow.com/questions/740816/open-link-in-iframe Commented Nov 16, 2015 at 17:37
  • Possible duplicate of How to make a target link to an iframe also scroll to it? Commented Nov 16, 2015 at 17:38
  • @MaxZoom more a duplicate of the one JCOC611 shared: open link in iframe Commented Nov 16, 2015 at 17:42

1 Answer 1

3

You can use an html link outside of the iframe, and set the target to the name of the iframe.

See the w3schools Example Here

<iframe src="demo_iframe.htm" name="iframe_a"></iframe>
<p><a href="http://www.w3schools.com" target="iframe_a">W3Schools.com</a></p>

If the link is inside the iframe, you simply need to set target=_self on the link.

<a href="http://www.w3schools.com" target="_self">W3Schools.com</a></p>
answered Nov 16, 2015 at 17:37
Sign up to request clarification or add additional context in comments.

4 Comments

I'm currently using that method, but isn't that the same as giving that a ID? If not, what is the difference between the name and id attribute because I need solid arguments if I'm going to do it differently than they think it should be done.
What do you mean by "differently than they think it should be done"? Your question states that you are not allowed to use ID.
ID is meant to be a unique identifier of an element in the DOM. Name is intended to identify the labels for form elements as seen in the POST, and also to identify the target of a form or hyperlink. See here: w3schools.com/tags/att_iframe_name.asp
Alright, it should be fine then, thank you for your help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.