The function linked to the button's onclick event is not being triggered

I am currently in the process of developing a webpage where, upon clicking a button, all elements within an HTML element are deleted and replaced with an iframe. However, I have encountered some issues as my code does not seem to be functioning properly. index.html:

<html id="html">
<head>
<script type="text/javascript" src="/freevpn/script.js"></script>
</head>
<body>
<input type="text" id="input" value="https://">
<button onclick="submit()">Go!</button>
</body>
</html>

script.js:

function submit() {
var submit = document.getElementById("submit");
var input = document.getElementById("input");
var iframe = document.createElement("iframe");
var html = document.getElementById("html");
iframe.setAttribute("src", input.value);
html.innerHTML = "";
html.appendChild(iframe);
iframe.requestFullscreen();
}

I am unsure why this functionality is not working as intended, but I believe there must be a solution. Any help would be greatly appreciated!

UPDATE: After troubleshooting, I identified a few issues such as linking problems with the JavaScript file, fixing the function, and ensuring all necessary parentheses were added. Despite making these updates, I am still unable to see any changes reflected on my webpage after clearing my cache. My website is integrated with Freenom, Cloudflare, Google Analytics, and Search Console - could this integration be delaying the update process? If so, is there a way to expedite this update?

Answer №1

Make sure you are using the function correctly.

Remember to include () when calling a function:

<button onclick="submit()">Go!</button>

function submit() {
  var submit = document.getElementById("submit");
  var input = document.getElementById("input");
  var iframe = document.createElement("iframe");
  var html = document.getElementById("html");
  
  iframe.setAttribute("src", input.value);
  html.innerHTML = "";
  html.appendChild(iframe);
  iframe.requestFullscreen();
}
<html id="html">
<head>
  <script type="text/javascript" src="/freevpn/script.js"></script>
</head>
<body>
  <input type="text" id="input" value="https://www.youtube.com/embed/_70Q-Xj3rEo">
  <button onclick="submit()">Go!</button>
</body>
</html>

Answer №2

Don't forget to include the parentheses when invoking your JavaScript function. Give this a try:

<button onclick="submit()">Go!</button>

If you need some extra help with the onclick Event, check out this useful resource:

Remember to place your <script> tag at the end of the body section (after the button in your code snippet). This ensures that the Javascript file is loaded after the HTML elements have been created and can be accessed by the DOM.

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

Developing several sliders and ensuring they operate independently of each other

I am currently in the process of developing multiple sliders for a website that I am building. As I reach the halfway point, I have encountered a problem that has stumped me. With several sliders involved, I have successfully obtained the length or count ...

Tips for Re-positioning the Manage Password Window in a Browser Using CSS

I am facing an issue with the Caps Lock key indication on my login page. When the Caps Lock key is on, a tooltip should be displayed below the password field. However, when the browser's "manage passwords" bubbles are shown, the tooltip gets hidden be ...

What is the best way to confirm only one click per browser?

Is there a way to limit rating functionality to allow users to rate only once per browser session? I am looking to implement validation for a rating component in React where users can only submit their rating once per browser. Below is an example of the ...

"Struggling to make the 'overflow: hidden' property work for an absolutely positioned

I'm struggling to conceal an absolutely positioned image within a CSS grid layout. Below is the code snippet: HTML: <div class="relative-parent"> <div v-for="item in 12" class="hiding-parent"> <div c ...

What's the deal with this error message saying val.slice isn't a function?

In the process of developing a web application using express with a three-tier architecture, I have chosen to use a mysql database to store blogposts as a resource. Here is an illustration of how the table is structured: CREATE TABLE IF NOT EXISTS blogpos ...

Adding an extra element to the <MuiThemeProvider /> causes the page to display as blank with no error notifications

After incorporating Material UI into my Meteor application via npm install --save material ui I successfully implemented the <Header /> component in my app.js file. However, when I try to add additional components, such as localhost:3000, all I see ...

A cautionary message about the React / TypeScript serviceWorker Update function and steps to troubleshoot

Whenever I attempt to run npm run build, an error pops up that says: Failed to compile. src/serviceWorker.js Line 38:36: Array.prototype.map() expects a value to be returned at the end of arrow function array-callback-return // Update a service work ...

Progressive bar functionality for file uploading in Ajax web API

I'm experimenting with this piece of code to send files to the server, Html Code: <input type="file" id="file1" name="browsefile" multiple="multiple" accept="video/mp4,video/*"> JavaScript Function: function FileUpload(SomestringParameter) ...

Transfer a parameter from a user input to control pagination in React.js

I am working on implementing a simple pagination function in a React 16 project. My idea is to pass the button's value to the API query to retrieve the page number and navigate to the next pages. I am utilizing the Unsplash API for fetching images. I ...

Guide on creating a function that accepts an Array of strings as a parameter and displays the initial letter of each element individually on separate lines

I am currently tackling my initial JavaScript assignment which involves the task of creating a function that accepts an array of strings as its parameter and outputs the first letter of each element individually on separate lines. The unique requirement ...

Keeping related items organized in Vue

I'm a newcomer to Vue and have carefully studied the documentation for Vue and Vuex. However, I am currently facing an issue which is hindering my progress. Although I believe I can figure it out eventually, I want to approach it with clean and best p ...

The rendering of HTML DOM is being obstructed on iPhone devices running iOS 13

Currently, I'm developing a web-based URL stream music player using JavaScript. The player functions smoothly on Android devices, but I'm encountering an issue with DOM rendering being blocked on iPhone devices. Despite rearranging the JavaScript ...

Utilizing Event Delegation in jQuery to Handle submit() Events

In my jQuery code, I am using the following: $(this).parents('form').submit(); Initially, it works without reloading the page the first time but subsequent calls result in the page being reloaded. While researching about this issue, I came acros ...

Troubleshooting Problems with Cookie and Session Management

I'm encountering an issue while trying to set cookies and session values during login on the backend, which is built in node js. However, when react calls the API to verify these cookies or session values, they are returning as undefined... My middle ...

Dragging and dropping files will not result in any redirection

My challenge involves working with a rectangle where I need to drop files. The issue is that when I try to drop them, the browser automatically redirects to the file location. While this behavior is expected, it interferes with my goal of uploading the fil ...

Displaying a dropdown menu from the top by utilizing the show() function

I've successfully implemented a dropdown menu using cssmenumaker. It works perfectly on desktop and mobile view. However, I'm facing an issue on mobile where the dropdown menu slides in from the left when clicked. I would prefer it to slide down ...

Two-sided vertical CSS menu

I'm struggling to design a layout with two vertical menus flanking the main content section. Despite experimenting with different combinations of inline, relative, and fixed positions, I can't seem to make it work. Here is the Fiddle link for re ...

Using JavaScript to organize and categorize data within an array

I am working with a multidimensional array and need to filter it based on the value at a specific index position. Here is what the array looks like: arr = [ [1 , 101 , 'New post ', 0], [2, 101 , 'New Post' , 1], ...

Executing Client-Side Function Upon Data Retrieval in a Node.js Application

Within my node.js Express application, there exists a route like this: router.get('/devices/:id/visualize', catchErrors(deviceController.visualizeDevice)); This route points to the following controller: exports.visualizeDevice = async (req, re ...

guide to setting up router access using token

I've received a token from the backend axios.post(process.env.VUE_APP_LOGIN, payload) .then(response => { const {access_token, token_type, user} = response.data; this.token = access_token this.$store.commit(&a ...