Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:43:46+00:00 2026-06-13T17:43:46+00:00

How do you completely replace a function in JavaScript? I got this code, but

  • 0

How do you completely replace a function in JavaScript?

I got this code, but it doesn’t work. The DOM gets updated, though. What’s up with that?

<html>
<head>
    <script id="myScript" type="text/javascript">
        function someFunction() {
            alert("Same old.");
        }
    </script>
</head>
<body>

<input type="button" onclick="someFunction();" value="A button." />
<script>
    function replace() {
        var oldFunctionString = someFunction.toString();
        var oldContents = oldFunctionString.substring(oldFunctionString.indexOf("{") + 1, oldFunctionString.lastIndexOf("}") );
        var newCode = "alert(New code!);";        
        var newFunctionString = "function someFunction(){"+newCode+"}";
        var scriptTag = document.getElementById('myScript');

        scriptTag.innerHTML = scriptTag.innerHTML.replace(oldFunctionString,newFunctionString);
    }

    replace();
</script>

</body>
</html>

JSfiddle here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:43:47+00:00Added an answer on June 13, 2026 at 5:43 pm

    Setting .innerHTML doesn’t re-execute a script. If you really wanted to do that, you’d have to create a new script element and append it to the DOM, which then overwrites what the previous script has done (not possible in all cases, of course).

    If you want to replace that function, just use

    somefunction = function() {
        alert(New code!); // syntax error, btw
    };
    

    Of course, to replace only parts of the code (not knowing all of it) you could try regex and co. Still just reassign the new function to the variable:

    somefunction = eval("("
      + somefunction.toString().replace(/(alert\().*?(\);)/, "$1New code!$2")
      + ")");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a jQuery function that is going to replace each hyperlink with a
String contains a bunch of useful methods such as String.replace(CharSequence, CharSequence) that are completely
Completely new to most of this stuff, but basically Im playing around with the
I'm working on a piece of JavaScript. I'm completely new to JS, But I
I have a Javascript plugin that searches the DOM for any elements starting with
I realize I am probably doing this completely wrong, but here is my issue...
I am dynamically creating an anchor that calls a javascript function. It works with
I have some javascript code like below function mask(eventVal,val){ var len = val.value.length; var
My JS: <script type=text/javascript> function DictionaryEntry() { var self = this; self.Simplified = ko.observable();
I need to edit or completely replace outline data (bezier curves) of OpenType fonts.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.