tags:

views:

484

answers:

5

Hi,

How to remove the contents of a Div Using JQuery. For eg.

 <div id="1">
  <label id="label1"/><br/>
  <input type="text" data-attr="phoneEuro" style="width: 200px;" id="input1"/> <label id="instr1"/>
 </div>

i want to remove the full content of the Div . and to add new contents to it.

+1  A: 
$("#1").html("");

or just

$("#1").html("your new content");

change id to start w/a letter

Jason
+2  A: 

First, according to the spec, id must start with a letter.

 <div id="myDiv">
  <label id="label1"/><br/>
  <input type="text" data-attr="phoneEuro" style="width: 200px;" id="input1"/> <label id="instr1"/>
 </div>


$('#myDiv').html('your new content');

If you use html method, you can simply override the existing DIV content.

SolutionYogi
"cannot" is a strong and - if you want to be strict - incorrect word. "should not" is probably more adequate.
Paolo Bergantino
According to spec, "ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")." Link: http://www.w3.org/TR/html4/types.html If you don't follow the guideline, your code may break in standards compliant browser.
SolutionYogi
I know the rules, Yogi, thanks. But all browsers out there will happily take an ID that starts with a number. As I said, you shouldn't, but you surely can.
Paolo Bergantino
Yes, but shouldn't we try to educate developers to follow the standards? A browser may take id which starts with a number but there could be a bug in certain browser under certain conditions where id starting with number behaves differently?
SolutionYogi
Which is why you _should not_ start IDs with numbers :) I'm not arguing the fact you need to educate any posters that are not aware of the rules, I was just being a little pedantic in the fact that you used the definitive "can not" when it's really just a best practice.
Paolo Bergantino
+4  A: 
$("#1").empty();

Also you shouldnt really start id's with a number

Darko Z
A: 
$("#1").html("<span class='red'>Hello <b>Again</b></span>");

From doc.jquery.com

lfx
A: 
$(document).ready(function()
{
    $('div#one').children().remove();
});
Keith Donegan