tags:

views:

200

answers:

5

Hi All,

I have a some problem

i.e i have a 60 text box controls in asp page i want to text box text to empty so , i am using like below

var st = document.getElementById("<%=hiddenrate.ClientID%>").value;//Total Control names

var controlnames = st.split(','); //split with comma 

var i = 0;
for (i = 0; i <= controlnames.length; i++)
{
    var gh = '' + '.SetText(' + "'Empty text'" + '' + ');';

    ft[i] + gh;

    //example rate1.SetText('');

    rate2.SetText('');
    '
    '
    rate60..SetText('');
}

but in javascript is that control name and property

How to set text as empty in total controls dynamically?

Thanking You,

Rajesh

+1  A: 

IF you want to clear the values of all text inputs use this code:

// get all <input> elements
var inputs = document.getElementsByTagName('input');

for (var i = 0; i < inputs.length; i++) {
    // check input type
    if (inputs[i].type === 'text') {
        inputs[i].text = '';
    }
}

You could easily modify that code to handle textareas as well or add some more filtering to the elements.

If you consider using JS framework you can make this code much shorter. For example if you use jQuery then here's the code for you:

$(document).ready(function () {
    $('input:text').text('');
});
RaYell
thanks for u r answer but it is not work for ASPX control propertiesand also document.getElementsByTagName('input'); not supported by aspx can u give code for aspx controls ?
karim
This is JS (client side language) which has nothing to do with ASP (which is server side)
RaYell
hi Ray Yell i tried your code but it is throwing an error Microsoft JScript runtime error: Object doesn't support this property or methodi am using ASPX (DevExpress Controls) it is not support what u mentioned code can u give any another way to do this? thanking you
karim
I added an alternative solution if you are willing to use jQuery framework in your app. In most cases using a framework (not necessarily this one, but it's quite popular) is a very good idea. All you need to do is include `jquery.js` to your page and start using it.
RaYell
i got the solutionThanking you u r help.
karim
+1  A: 

Here is another sample to clear value all textboxes:

<script type="text/javascript">
   function pp(){
          for(p in form1.childNodes) {
              if(form1.childNodes(p).type=="text")
                  form1.childNodes(p).value="";
          }
    }
</script>

<body>
  <form name="form1">
      <input type="text" name="a1"/>
      <input type="text" name="a2"/>
      <input type="text" name="a3"/>
      <input type="button" name="a11"/>
      <input type="submit" name="a12"/>
      <input type="button" value="Clear" onclick="pp()" name="a13"/>

   </form>
</body>
adatapost
A: 

Thank You For Ur quick response, but i'm using aspx controls

Thanking You, Rajesh.

A: 

How about simply calling document.forms["form1"].reset() in Javascript? It will clear the values of all the controls in the form.

Matthew Lock
sorry matthew Lock i want only some text boxes in aspxcan you give any code for dynamically change control names.examplecontrol1.Settext('');control2.Settext('');""control50.Settext('');control'Dynamicnubmers' .settext('');thanks matthew
karim
A: 

Thanking u all i got the solution like below method var i = 0; for (i = 0; i <= ft.length; i++) {

               x = new Object();
               x = ft[i];
               propertyName = ".SetText";
               propertyValue = " ";
               if(x !="undefined")
               eval(''+x+'' + propertyName + "('" + propertyValue + "');");
               or

              eval(x).SetText('');
            }

thnks to all.

karim