With jQuery you can add and remove HTML elements on a page, and modify their content.
$('<tag>New Content</tag>');jQuery parses the HTML into a DOM fragment and selects it, just as an ordinary jQuery selector does. That means it's instantly ready for further jQuery processing.
$('<tag>New Content</tag>').addClass('class_name');
<style type="text/css"><!-- .spn { color:blue; font-weight:bold; } --></style> <script type="text/javascript"><!-- $(document).ready(function() { var new_btn = $('<button id="btn">Click</button>'); // new button var new_span = $('<span>START</span>').addClass('spn'); // new <span> with class="spn" new_btn.insertAfter('#idd'); // insert the new button after the tag with id="idd" // now we use the new added button, when is clicked $('#btn').click(function() { // insert the "new_span" at the beginning inside all DIVs with class="cls" new_span.prependTo('div.cls'); }); }); --></script> <div class="cls"> Create and insert new HTML items with jQuery</div> <div class="cls" id="idd"> coursesweb.net</div>The code above creates a <button> and a <span> tag to which adds a CSS class, inserts the button into the page, then uses it to register a click event that will add the <span> inside all <div> tags with class="cls", when the new button is clicked.
Element.attr('attributeName', 'value');
<style type="text/css"><!-- .spn { color:blue; font-weight:bold; } --></style> <script type="text/javascript"><!-- $(document).ready(function() { $('#idd').after('<button id="btn">Click</button>'); // insert the new button after the tag with id="idd" // now we use the new added button, when is clicked $('#btn').click(function() { // insert a SPAN tag with class="spn" at the end in all DIVs with class="cls" $('div.cls').append('<span class="spn">END.</span>'); }); }); --></script> <div class="cls">Adding directly new HTML items with jQuery </div> <div class="cls" id="idd">coursesweb.net </div>As you can notice, this time the class="spn" is directly included in the tag, without using addClass() method.
$('selector').remove('select');
- "select" - is an optional parameter that can be used to specify what to remove.<script type="text/javascript"><!-- $(document).ready(function() { // when the tag with id="btn" is clicked $('#btn').click(function() { // removes all LI with class="cls" in OL $('ol li.cls').remove(); }); }); --></script> <ol> <li class="cls">List with class="cls".</li> <li>List without class.</li> <li class="cls">Another List with class="cls".</li> </ol> <button id="btn">Remove</button>Click the "Remove" button to see the result (will delete all <li> tags with class="cls" in <ol>).
$('Element').html();
$('Element').text();
$('Element').html('<tag>New Content</tag>');
$('Element').text('New text content');
<script type="text/javascript"><!-- $(document).ready(function() { // when the tag with id="btn" is clicked $('#btn').click(function() { // gets the content of "#div1", with html(), and text() var dv_html = $('#div1').html(); var dv_text = $('#div1').text(); // displays the values in an alert window alert('html - '+ dv_html+ '\n text - '+ dv_text); // modify the content in "#div1" and "#div2" $('#div1').html('New content, changed with <u>html()</u>'); $('#div2').text('New content, modified with <u>text()</u>'); }); }); --></script> <div id="div1">div1 <b>content</b>.</div> <div id="div2">Another <u>HTML content</u></div> <button id="btn">Click</button>As you can notice, if you click the button bellow, when you get a content with text(), it removes the tags. When you add a HTML, the element will contain and display the entire content string exactly as defined, including the <tags>.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net