0

Is it possible to clear all asp:PlaceHolder controls on client side using JavaScript?

Something like: placeHolder.Controls.Clear() - but this is on server side.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Inbal
  • 909
  • 2
  • 28
  • 46

2 Answers2

0

you can use following jQuery code:

$("#placeHolder").html("");
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
0

Not elegant, but works. Surround your PlaceHolder with another div:

<div id="masterDiv">
        <asp:PlaceHolder runat="server" ID="placeHolder1" >
            Some stuff
            <input type="text" />
            <input type="submit" value="Don't click!" />
        </asp:PlaceHolder>
 </div>

If you can use jQuery, then use the empty function or html('')

$('#masterDiv').empty();

Or

$('#masterDiv').html('');

If you must use JavaScript, then do same:

document.getElementById('masterDiv').innerHTML = "";
Andrei Drynov
  • 8,362
  • 6
  • 39
  • 39
  • Thanks for your reply. I can use JQUERY, I tried both solutions but it doesn't work. To make things clearer, I want to clear only the controls inside the asp:PlaceHolder. Based on your example: – Inbal May 28 '12 at 10:18
  • Did you want to clear the contents of those controls or do you want to just clear the whole placeholder of any controls so that you are left with
    (the latter does work)?
    – Andrei Drynov May 28 '12 at 13:05
  • I want to clear the placeholder controls (the controls themselves, not just their content), but I don't want to remove the placeholder from the page. Exactly like - placeholder.controls.clear() function, but in client side. – Inbal May 29 '12 at 06:45
  • This topic has many suggestions: http://stackoverflow.com/questions/6364289/clear-form-fields-with-jquery – Andrei Drynov May 29 '12 at 10:56
  • Thanks Andrei, but I don't want to clear textbox text, for example.. I want to remove the textbox itself from the place holder. – Inbal May 29 '12 at 11:30
  • Placeholder typically creates a DIV, so you can run $('#masterDiv div').empty(); to remove controls from it. – Andrei Drynov May 29 '12 at 15:35