Click here to Skip to main content
15,921,179 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
i need to make a clear function !
i have DIV FULL OF INPUTS , DDL's , CheckBoxes , etc ...
i need to set all its values to null after every operation !
i've tried this :

C#
me.claerControls = function (id) {

       $("#" + id + "[type='text']").val('');
       $("#" + id + "[type='radio']").prop('checked', false);

   };



lets say im passing "#UserEdit" as an ID why the values are not being cleared ? when calling the function what did i miss here ?
Posted
Comments
Sergey Alexandrovich Kryukov 7-Sep-15 14:32pm    
Why doing so? Why null? What's the problem?
What have you tried so far?
—SA
Moykn 8-Sep-15 10:15am    
Guess he's trying to reset values.
Sergey Alexandrovich Kryukov 8-Sep-15 10:47am    
Then it's hardly null.
—SA
Moykn 8-Sep-15 10:50am    
I know, maybe he just can't explain himself properly.
But, based on his code, i think he's trying to reset values.
Sergey Alexandrovich Kryukov 8-Sep-15 10:55am    
I understand. The value depends on control. It could be "", false, and so on...
What's the problem? Just do it. You actually can add your reset functions to all controls, depending on their types, and then call it in some loop or recursive method. You can even add such function to control prototypes.
—SA

Ok, the problem here is the meaning of your selector.
JavaScript
me.claerControls = function (id) {
    $("#" + id + "[type='text']").val('');
    $("#" + id + "[type='radio']").prop('checked', false);
};


What you have written here is the same as:
JavaScript
$("#UserEdit[type='text']")

and this mean "give me a node whose id is #UserEdit AND type=text"

I think you can do what you want this way:

JavaScript
me.clearControls = function (id) {
     var $container = $("#" + id); //Select the div first, so this will "shorten" the path to elements inside it
     $(":text", $container).val(''); //Give the container div as a context
     $(":radio", $container).prop('checked', false).change(); //maybe you'll need to trigger a "change" event, after changing prop's value, this could be done like in this line.
     $(":checkbox", $container).prop('checked', false).trigger("change"); // I prefer to use trigger
     $('select', $container).prop('selectedIndex', 0);
};
 
Share this answer
 
v2
Comments
May Bash 8-Sep-15 14:44pm    
you helped ALOT thank you so much :)
The below should work.
$('#' + id + ' input:text');
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900