How does Javascript interact between input text box and iframe when onblur event occurs?

I'm having trouble entering a URL into a form text input and then updating the contents of an included iframe with the specified webpage when onBlur event occurs. I've been struggling to get it to work and can't seem to figure out what's wrong...

Could anyone offer some assistance?

Here is the code snippet...

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” 
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<script language="javascript" type="text/javascript" >
<!-- hide
function setIframeSource() {      
var theSelect = document.getElementById('location');      
var theIframe = document.getElementById('myIframe'); 
var theUrl;        
theUrl = document.getElementById("location");
theIframe.src = theUrl;
}
// end hide -->
</script>
</head>
<body><center>
<form id="form1" method="post">      
<input type="text" style="width:150px;" name="location2" id="location" onBlur="setIframeSource();">
</form>  
<table width="100%" height="100%" border="1">
<tr>
<td width="300"><iframe src="http://www.ibm.com" id="myIframe" width="280" height="300"   name="frame1"></iframe></td> 
</tr>
</table>
</center>
</body>
</html>

Answer №1

function changeIframeSrc() {      
    var locationSelect = document.getElementById('location');      
    var iframeElement = document.getElementById('myIframe'); 
    var urlValue;        
    urlValue = document.getElementById("location").value;
    iframeElement.src = urlValue;
}

It seems like the issue lies in line 5 above -- it should be:

urlValue = document.getElementById("location").value;

Currently, you are assigning the input element itself to urlValue, which is not a valid value for iframeElement.src!

Answer №2

Give this a shot once you've established the iframe source:

theIframe.contentDocument.location.reload(true);

Additionally, retrieve the input value like so:

document.getElementById('location').value;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling to use the innerHTML method to update a div within another div element?

<!DOCTYPE html> <html> <head> <title>Business Information Card</title> <script type="text/javascript"> window.onload = init; function init(){ var button = document.getElementById("populate ...

Utilizing the rewire module in Node.js to assign a JSON value

//filejson.json { "body": { "name":"abc" } } //mainFile.js var readJson = require("/filejson.json") var req=clone(readJson.body); I am looking for a way to dynamically change the value of the name in my JSON data without actually editing the .jso ...

Is there a way to verify the existence of an Array Field?

For JavaScript, the code would look like this: if (array[x] != undefined && array[x][y] != undefined) { array[x][y] = "foo"; } Is there an equivalent simple method for achieving this in Java? I have tried checking if the field is null or no ...

Tips for Successfully Sending Vue Data in Axios POST Call

I'm struggling to pass Vue data to an axios.post request. Using the Vue template doesn't seem to work. How can I successfully pass the Data? Here is my Code: <body> <div id="app" class="container"> <div> ...

Interactive Checkbox Generated in Jquery UI Accordion Header

I need help creating an accordion with checkboxes in the headers dynamically populated from a database. I have tried using the click() function to prevent the accordion from activating when checking the checkbox, but since the accordion is generated dynami ...

What is preventing Web API from triggering a CORS error in browsers such as Chrome and Edge, as well as the Postman tool?

While working on developing an API in Asp.Net Core 3.1, everything seemed to be functioning properly. However, I encountered CORS-related errors when attempting to send requests via ajax. Interestingly, these errors were not present when sending GET reques ...

Tips for hiding a div element until its visibility is toggled:- Set the display property of

Looking for some guidance on this jQuery code I'm working with to create a toggle menu. The goal is to have the menu hidden when the page loads, and then revealed when a button is clicked. However, currently the menu starts off being visible instead o ...

Unable to view new content as window does not scroll when content fills it up

As I work on developing a roulette system program (more to deter me from betting than to actually bet!), I've encountered an issue with the main window '#results' not scrolling when filled with results. The scroll should always follow the la ...

Tips for resolving rendering page issues in an express app

My application is a straightforward blog platform that showcases a schema for the title, entry, and date of each blog post. There is also an edit/delete feature that is currently under development. When attempting to use the edit/delete button on a selecte ...

The Vue.js updated hook causing unnecessary page rerenders even when there are no changes in the state

Seeking to retrieve data from an api and utilize updated() to trigger a rerender upon a change in the state that stores the fetch url. However, encountering an issue where the updated hook continues to render even when there is no alteration in the state. ...

Avoid shifting focus to each form control when the tab key is activated

I have a form where users need to be able to delete and add items using the keyboard. Typically, users use the tab key to move focus from one element to another. However, when pressing the tab key in this form, the focus only shifts to textboxes and not t ...

Safari browser experiencing issues with Google Maps API functionality

Whenever I perform the following actions: var map = new GMap2(document.getElementById(mapCanvas)); var directions = new GDirections(map); directions.load("INSERT DIRECTIONS HERE"); Everything functions properly when using Firefox on a Linux platform, but ...

Exploring textboxes with jQuery loops

Is there a way to clear all these textboxes by iterating through them using Jquery? <div class="col-md-4" id="RegexInsert"> <h4>New Regex Pattern</h4> <form role="form"> <div class="form-group"> &l ...

What is the best way to generate a consistent and unique identifier in either Javascript or PHP?

I attempted to search for a solution without success, so I apologize if this has already been discussed. Currently, I am in need of an ID or GUID that can uniquely identify a user's machine or the user without requiring them to log in. This ID should ...

Creating mobile-friendly websites for a variety of devices, including smartphones and tablets

Creating a prototype webpage for my friend, one crucial aspect is ensuring it looks good on different devices. Utilizing Bootstrap, I've implemented vertical collapsing for certain elements, but I'm struggling with how to adjust other components ...

Nuxt.JS is not rendering blocks in the way that I require

I am currently developing a nuxt.js SSR web application and encountered an unusual issue. <div class="container" @mouseenter="hovered = true" @mouseleave="hovered = false"> <transition name="fade"> <div class="class1" v-show="!hovered ...

For handling the onSubmit event, utilize both axios and axios-mock-adapter

I am attempting to utilize both axios and axios-mock-adapter in a single location to consolidate multiple mocks, then export the axios instance for use elsewhere: axios.js import axios from 'axios'; let api = axios.create({ baseURL: 'h ...

Access a folder in JavaScript using Flask

I need to specify a directory in a script. $images_dir = '{{url_for('.../pictures')}}'; In my flask application directory structure, it looks like this: Root -wep.py -templates -gallery.html -static -pictures The images are stored ...

Transmitting the character symbol '&' within a string from Angular to PHP

My goal is to transmit a string, such as "the cat & the dog", from Angular to PHP using GET method. I have used encodeURI(note) in Angular and in PHP $note = $_GET['note']; $note = mysql_real_escape_string($note); However, when it gets inse ...

What is the best way to attach an identifier to the URL of an ajax request in Vue.js?

Here is the URL for the page: To obtain JSON data, I am trying to retrieve the id from the URL and append it to my Ajax request. I extracted the id using the following code: var id = window.location.href.split('=').pop() console.log(id) My Vu ...