views:

63

answers:

3

Hi,

For example I have this HTML code:

<div id="canvas">
    <div id="root">
        <div id="content">
            <span>
                <title>My SVG Example Title</title>
                <ellipse id="svg_1" />
            </span>
        </div>
    </div>
</div>

How can I innerHTML the <span> tag without changing the <title> tag using jQuery? So my expected output would be something like this:

<div id="canvas">
    <div id="root">
        <div id="content">
            <span>
                <title>My SVG Example Title</title>
                <input type="text" value="this is the innerHTML I must insert but how?" />
            </span>
        </div>
    </div>
</div>
A: 

Remove the ellipse element and add your input element:

$("#content span ellipse").remove()
var input = $("<input>").attr("type", "text").attr("value", "foo");
$("#content span").append(input);
Chris Schmich
well, you're right. but he didn't mentioned that the structure is always like this.
jAndy
+3  A: 
$('#canvas')
 .find('span')
 .contents()
 .not('title')
 .replaceWith($('<input />', {
    type: 'text',
    value: 'this is the innerHTML I must insert but how?'
 }));
jAndy
Nice. Thanks jAndy and to all who replied :)
marknt15
+2  A: 

I would do this:

var newHtml = '<input type="text" value="NewStuff!" />';

$("#svg_1").after(newHtml).remove();
Sohnee