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

  • SEARCH
  • Home
  • 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 8827015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:16:56+00:00 2026-06-14T07:16:56+00:00

I have an ajax call which returns me following response: <div class=content> <script language=JavaScript>

  • 0

I have an ajax call which returns me following response:

<div class="content">
    <script language="JavaScript">  
        function validate(){
            alert('validate is working');
        }
    </script>
    <div class="container">
        <a href="#" onclick="javascript:validate();">my button</a>
    </div>
</div>

Now, i want to convert the response into html and append it to the page, which made the ajax call.

$.get('test.php', function(data) {
    $('#someTarget').append($(data).find('.content').html());
});

I have tried above code, but it seems this only append container div. Is there a way to append javascript code block into my existing page?

  • 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-14T07:16:57+00:00Added an answer on June 14, 2026 at 7:16 am

    In your code are two problems:

    $('#someTarget').append($(data).find('content').html());
    
    1. $(data) does parse and execute your script, before you inserted it into DOM

    2. assuming you meant $(data).find('.content').html() the html() returns the innerHTML of content, you only need $(data).

    Try this:

    <div class="content">
        <style type="text/javascript">  
            function validate(){
                alert('validate is working');
            }
        </style>
        <div class="container">
            <a href="#" onclick="javascript:validate();">my button</a>
        </div>
    </div>
    

    Now there is a trick: I’m using <style> to wrap scripts, that should be executed after interpreting and inserting into DOM.

    $.get('test.php', function(data) {
    
        // parse HTML to DOM
        var $data = $(data);
    
        // take the scripts out
        var $inlineScripts = $('style[type="text/javascript"]', $data).remove();
    
        // Now append the DOM
        $('#someTarget').append($data);
    
        // And globalEval the scripts
        $inlineScripts.each(function () {
            $.globalEval(this.innerText);
        });
    });
    

    Okay for your simple validate function that gets called by a click handler, you currently don’t need my trick for inline scripts, however you will need it, when you have active scripts, that should be executed after the HTML is parsed and inserted.

    Update: If you don’t want the ‘.content’ to be inserted, don’t use html() – because it returns a string, so you do parse the html twice. Use instead .append( $(".container", $(data)) ) or .append( $(data).find(".container") ).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following ajax call in my JavaScript code url = 'http://news.ycombinator.com/?callback=?'; $.ajax({url:url ,async:!1,dataType:'script',
Let's say for example, I have the following javascript function that returns a boolean:
I have the following JavaScript (I'm using jQuery): function language(language) { var text =
I have an element which autosaves its contents via an AJAX call. Currently, whenever
On my main page I have this jquery code which does an ajax call
I have an ajax call that fires multiple times but is called once. function
I have a jQuery ajax call that returns html of a table. Now I
I have an asp.net asmx webmethod that returns the following response: <?xml version=1.0 encoding=utf-8?>
Within a Flask app, I have the following ajax call: $.ajax({ url: {{ url_for(
I have a simple jquery ajax call which posts a car manufacturer and responds

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.