0

I have a select field and function to hide/show divs. Inside those divs are input fields that I want to reset each time a different selection is made.

This script clears fields as long as I specify each field ID.

<script>
$(document).ready(function(){
    $("#Main_Connection").change(function(){
    $('#InputFieldID').val("");
        $(this).find("option:selected").each(function(){
            var optionValue = $(this).attr("value");
            if(optionValue){
                $(".box").not("." + optionValue).hide();
                $("." + optionValue).show();
            } else{
                $(".box").hide();
            }
        });
    }).change();
});
</script>

But I also made this script to get all the field IDs, which I think works because it shows all the IDs in the browser debugger.

<script type="text/javascript">
    $(document).ready(function ()
    {
    var AllFields = $("[id^=Main_] input");
    console.log(i, "Hello, world!");
    });
</script>

Simply changing $('#InputFieldID').val(""); to $('#AllFields').val(""); does not work. How can I get my first script to reset each field found by the second script?

4
  • 1
    can you show the html here? I'm note sure what your fields are named. You shouldn't have more than one element with the same id. Commented May 28, 2020 at 2:31
  • 1
    If your jQuery selector properly selects all the <input> fields to be cleared, this should clear them: $("[id^=Main_] input").val(''). Commented May 28, 2020 at 2:44
  • Thanks @kmoser, it was that simple. Unfortunately I cannot mark your reply as the answer. Commented May 28, 2020 at 4:32
  • @sjaak I've added my comment as an answer. Commented May 28, 2020 at 4:45

2 Answers 2

1

If your jQuery selector properly selects all the <input> fields to be cleared, this should clear them:

$("[id^=Main_] input").val('')
Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure if this is 100% what you are looking for , but : try using each around all the IDs as follows :

$("[id^=Main_] input").each(function() {
     $(this).val("");
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.