1

I am parsing a html string fragment and changing the src attribute of image tags. (i.e. replacing paths that start with ".."

var hstr = $.parseHTML(str);
$(hstr).find('img').each(function(e){
var srcvalue = $(this).attr('src');
srcvalue = srcvalue.replace(/../gi, "");
$(this).attr('src', srcvalue);
});

then setting the contents of a div element by appending the result

document.getElementById('#section').append($(hstr));

but it is not working... can anyone tell me what is going wrong ?

1
  • your current code can be simplified to: pastebin.com/yV4CLd9P (implemented answers from below into it as-well to simplify things) Commented Mar 6, 2013 at 19:28

3 Answers 3

3

Dont mess up pure JS DOM and Jquery. Use $('#section').append($(hstr))

Sign up to request clarification or add additional context in comments.

Comments

2

Don't get confused with Jquery (#ID) selector, Use either

$('#section').append($(hstr));

or (no need of # and use appendChild in DOM element)

document.getElementById('section').appendChild($(hstr));

1 Comment

Thanks for pointing out append() not works in pure JS. I just didn't notice it.
1

You should append the manipulated object, you are creating another object, also DOMElement object doesn't have append method.

$(hstr).find('img').each(function(e){
    // ...
}).appendTo('#section');

Using prop method:

$(hstr).find('img').prop('src', function(index, srcValue) {
    return srcValue.replace(/../g, "");
}).appendTo('#section');

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.