0

I have to develop a search function in javascript such that it will display a message/pop-up window when users does not select an option in dropdownlist. I am using usercontrols in my project. so the dropdownlist's are in .ascx file and my search function will be in .aspx file. Here is the code which I am using:

function Search() 
    {
        var src_status = createObj("bodyuc_drp_Status").value;
        var src_program = createObj("bodyuc_drp_program").value;

        if(document.getElementById(src_program).value == 0 && document.getElementById(src_client).value == 0)
         {
            alert("Please select atleast one client or program")
            return false;
         }
        else {
            createObj("hdn_search").value = "Search";
            return true;
        }
    }

the value '0' in the if condition is the index of '--select one--' option in the dropdownlist. The above alert message should be displayed when the index is 0 otherwise the user should get the data based on his selection.

This code gives me alert message even if the users select a value other than the 'select one' option. Can anyone tell my why?

really appreciate your help.

1 Answer 1

1

The value property of the select element is the same as the value property for the selected option element, not its index. To get the index, use the selectedIndex property:

if(document.getElementById(src_program).selectedIndex == 0 && document.getElementById(src_client).selectedIndex == 0)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the replly. Really appreciate it.

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.