javascript - How would I make changes to an html string? -


i'm reading api returns html string. , i'm using knockout bind html content of div.

what want make changes anchor elements in string before bind it.

for example let's response.

"<div>lorem ipsum <a href='#'>link1</a> more random text</div>"

and want change anchor text else.

this i'm trying right now

var htmlstring = "<div>lorem ipsum <a href='#'>link1</a> more random text</div>"; var links = $(htmlstring).find("a"); $(links[0]).text("hello world"); $("#stringtarget").html(htmlstring); 

and it's not making update.

note: example isn't using knockout.js, it's illustrate process of editing string setting html of div altered string.

here fiddle of example

you can use like

var htmlstring = $("<div>lorem ipsum <a href='#'>link1</a> more random text</div>"); var links = $(htmlstring).find("a").text("hello world"); $("#master").html(htmlstring); 

you should use $() make object

demo


Comments