0

when my html elements are stored in variable then how can i access any html element from the variable. suppose if i need to extract or manipulate div called "dv1" or dv2. may be i may need to insert row in table mytab. then how can i do it? please help me with sample code.

$("#btnPrint").click(function () {
sHtml = "<div id='dv1'><table id='mytab'>";
                sHtml += "<tr><td>" + "<img src='" + ImgPath + "' border='0'/>" + "</td></tr>";
                sHtml += "<tr><td>" + $('#lblTxt').html() + "</td></tr>";
                sHtml += "</table><div id='dv2'>my content here</div></div>";                
            return false;
        });

thanks

2 Answers 2

2

You can convert the string to a jQuery object, and then manipulate it.

$html = $(sHtml);
$html.find('#dv2').text('Test');

$html.find('#mytab').append('<tr><td>Test</td></tr>');
Sign up to request clarification or add additional context in comments.

Comments

2

Pass your html string to jquery to transform it in a jQuery object that you can manipulate as usual.

var sHtml = '<div id="mydiv"><p>some html code ...</p></div>';
var $sHtml = $(sHtml);
$sHtml.find('#mydiv').append('<p>something</p>');

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.