Edit HTML and CSS in live mode while also running JavaScript for instant enhancement

Recently, I created a script that allows me to build a web page in real-time. I can input HTML and CSS codes without any issues, but unfortunately, I am unable to execute JavaScript due to the limitations of the string-based editor I am using. Can anyone offer some guidance on how to enable JavaScript execution in this editor?

It's worth noting that this simple script allows me to input an entire webpage's code with CSS and HTML seamlessly, without any need for escaping characters, resulting in a formatted page. However, JavaScript functionality is not supported by this script.

function html() {
var str = document.getElementById("go").value;
  document.getElementById("show").innerHTML = str;   
}

   
<p id="show"></p>
<textarea id="go" onKeyUp="html()"  width="100%" cols="50" rows="10">
   </textarea>

UPDATE: Exciting news - I finally found a solution that allows this editor to execute JavaScript as well! Now, I can create a complete webpage with JavaScript functionalities integrated. Huge thanks to everyone who provided assistance!

$(document).ready(function(){
  $('#editor').keyup(function(){
      $('#page').html($(this).val());
  });
   });
<p id="page"></p>
 <textarea id="editor"  width="100%" cols="50" rows="10" placeholder="you can use HTML, CSS, and JavaScript here..."></textarea>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Answer №1

All of your content is being placed within paragraph tags, which is why any content within script tags is not functioning properly. Consider appending everything within the body tag instead:

function html() {
  var str = document.getElementById("go").value;
  document.getElementsByTagName("body")[0].innerHTML = str;   
}

Could you please provide more clarity on your request to "find a way to execute javascript in this editor?" What specific outcomes are you hoping to achieve?

Answer №2

Executing JavaScript code automatically is not recommended, as it poses security risks. An alternative approach is to use the eval() function to run the code. Here's a sample implementation:


Although using eval() increases security risks, it may be necessary in certain scenarios.

function displayContent() {
var inputContent = document.getElementById("inputField").value;
  document.getElementById("output").innerHTML = inputContent;   
}

function executeCode() {
var codeToRun = document.getElementById("codeField").value;
  document.getElementById("output").innerHTML = eval(codeToRun);   
}
<p id="output"></p>
<textarea id="inputField" onKeyUp="displayContent()"  width="100%" cols="50" rows="10"> </textarea>
<textarea id="codeField" width="100%" cols="50" rows="10"> </textarea>
<br><button onclick="executeCode()">Run Code</button>

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

Error: The property 'open' is not defined in the mdMenu object

Encountered a bug while working with angular-material design... When using md-menu, if a sub menu item is opened (as shown in the image) and then hovering over a non-subMenu item (menu item), it throws an error "Cannot read property 'open' of nul ...

Is there a way to refresh the current page in Ionic 3 when the browser is reloaded?

I have encountered a problem in my work with an Ionic 3 App. I am looking for a solution where the browser will refresh the current page instead of redirecting me to the home page. Is this achievable? I attempted to solve it using the following code: thi ...

Is there a way to move the submit form button to the next line using CSS?

I am having trouble centering the submit button below two input fields in a simple React form. Can anyone offer assistance with this? HTML .SearchBar-fields { display: flex; justify-content: center; margin-bottom: 2.88rem; flex-direction: row; ...

Using Vue3 to set attributes on web components

I recently developed a self-contained web component using Vite and Vue3 to replace outdated jQuery libraries. When this component is rendered in HTML, it appears like this: <custom-datepicker id="deliveryTime"> #shadow-root <div> ...

Manipulate the text within a canvas element using JavaScript

In this scenario, suppose json.mes represents the received message from Ajax. There is a button implemented to display the message in a canvas box. Although I have attempted to retrieve the message using getElementById (as shown below), it seems to be in ...

Error 500: Issue with JQuery AJAX File Upload

Hey there, I'm facing an issue with doing a file upload using JQuery's AJAX feature as I keep getting the error 500. $(function() { $( 'form' ).submit ( function() { $.ajax({ type: &a ...

Creating an error handler in PHP for dynamically adding or removing input fields is a crucial step in ensuring smooth and

I designed a form with multiple fields, including a basic input text field and dynamic add/remove input fields. I successfully set the first field as required using PHP arguments, but I'm struggling to do the same for the additional fields. I need as ...

Using AJAX to filter SQL query results based on non-empty fields in a submitted form

I am looking to implement AJAX functionality to transmit data from a form to my PHP file, which will then execute a query on the database and return an HTML table to refresh the main page with the requested information. This process should occur in "real- ...

Refreshing JWT Authentication in Angular

I am currently following a tutorial on Egghead.io, which can be found here. However, I am adding a MongoDB to fetch my users which is causing me some issues. I have managed to get everything working except for the part where it mentions that the /me route ...

Minimizing repeated autofocus calls in material-ui's <TextField> component

In the realm of coding with material-ui, when dealing with the <TextField> component, it's important to keep in mind that the solution may actually lie within React itself. Let's paint a scenario where we're crafting a basic login for ...

The style from 'http://localhost:2000/cssFile/style.css' was rejected because its MIME type was 'text/html'

Currently, I am attempting to include my style.css file in the home.ejs file being rendered by express.js. However, I keep encountering the following error: Refused to apply style from 'http://localhost:2000/cssFile/style.css' because its MIME t ...

I require the flexbox children to be arranged in a regular manner

How can we set flexbox children to default layout behaviors? Let's make them behave like the disobedient kids they are. .wrapper { This should be a row } .column { Make all heights equal display: flex; } .child { Back to regular display...wit ...

AngularJS is restricting the use of square brackets within the URL parameter, specifically the character '[.'

My goal is to connect to an external API Everything works smoothly when my parameters are set up like this $http.post('http://api.myprivatebox.com/users.json', { email : email, password : password}).then(function (results) { console.log( ...

Creating an HTML Canvas using an AngularJS template

I am facing an issue with rendering html elements on a canvas using html2canvas JS. I am utilizing AngularJS for data binding on the html page, but unfortunately, the dynamic data is not showing up on the canvas generated from these html elements. For inst ...

Keep sending HTTP requests until the header contains an attachment

Welcome to our web application where you can generate and download a .zip file simply by visiting the URL. I am looking to develop a Node.js application using the requestjs library that will continuously send requests until it detects an attachment header ...

Troubleshooting Bootstrap Social Buttons: Background Color and Hovering Challenges

I've been working with Bootstrap to design social buttons for Facebook and LinkedIn. However, I'm facing an issue where setting the background color to white for the buttons doesn't cover them properly. The white background extends beyond th ...

Uploading a collection of objects to Node.js for storage in a database with Mongoose

I have developed a ReactJS form that allows users to dynamically add form "parts" (sections with form input fields). Let me illustrate this concept with an example: <div> <input placeholder="Author" /> <input placeholder="Age" /> ...

Utilizing Angular's global interceptor functionality can streamline the process

Having trouble making 2 interceptors (httpInterceptorProviders, jwtInterceptorProviders) work globally in my lazy modules. I have a CoreModule and X number of lazy-loaded modules. Interestingly, autogenerated code by the Swagger generator (HTTP services) g ...

Include a flexible div in the hero section and change the position when resizing the screen

Looking to achieve a specific layout with only two resolutions in mind (767px minimum and maximum). For screens larger than 767px, display two divs side by side: one with text on the left and the other with an image on the right. The text remains in a fi ...

Pass the PHP data back to my existing webpage for JavaScript to retrieve

I recently set up a form on my WordPress website for users to submit data. Once the form is submitted, I use AJAX to create a new post without having to reload the page. I now need to figure out how to pass the post ID, a simple integer, back to the page s ...