0

I want to load some html from server, I store the loaded html in a string then I want to modify the values of certain tags and elements within that string before appending it : here is how I'm trying to do it :

script of test1.html :

<head>....</head>
<body> <div id="main"></div></body>
<script>
    $(document).ready(function(){
        $.get("test2.html").done(function(data){
            $("#rf", data).val("new value");
            $("#main").append(data);
        });
        });
</script>

test2.html

<p id="rf"> <b>old value</b></p>
1
  • 2
    And the problem you're having is.....? Commented Jul 16, 2014 at 16:04

2 Answers 2

1

The first problem is trying to target the val() method of a paragraph. That will not do anything as it has no val property to return. You need to use text or html to replace the content.

Second, convert the HTML string to a DOM tree first with $(data) (see notes below as to why I use a dummy div and html() instead), then find the element, change it etc then append the new tree to the target:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/NWj62/1/

var html = '<p id="rf"> <b>old value</b></p>'

$(document).ready(function () {
    var $html = $("<div>").html(html);
    $html.find("#rf").html("new value");
    $("#main").append($html);
    //$.get("test2.html").done(function(data){
    //    $("#rf", data).val("new value");
    //    $("#main").append(data);
    //});
});

You need to wrap the incoming HTML in a dummy div as find will not match the top element of the tree.

I substituted dummy data so you could see it working without the ajax call.

Note: $(htmlstring) will collapse html and body tags into a flatter structure than you might expect, but your example only has the paragraph so is fine.

Your code will be something like:

$(document).ready(function () {
    $.get("test2.html").done(function(data){
        var $html = $("<div>").html(data);
        $html.find("#rf").html("new value");
        $("#main").append($html);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, but The purpose is to do it for a more complex html file with many divs and ids, this one was just an example
@Tarik Mokafih: The same pattern applies to any number of changes. $html.find("something").html("somethingelse") etc You show me more change, I will show you more examples... We can only work with what we are shown :)
0

You want some sort of template functionality, since getting HTML from server and transforming it into a DOM tree and then applying manipulations manually is a lot of code repetition for nothing. Also, it's relatively expensive to do dynamic tree manipalations.

Either the html is processed on the server or on the client side, is your choice.

  • Backend templates: depends on your backend framework (ie. Django has its own template module).
  • Fronted templates: You can use Underscore templates or Handlebars templates (more similar to Django templates).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.