Praveen k. Agrawal
Praveen k. Agrawal

Reputation: 449

Convert the javascript function into jquery

I have one JavaScript function which are set drop down and textbox from this function, I wanted to convert this function into jQuery kindly assist.

function setDDls(strCity, strState, strCountry) {
    $(txtCity).val(strCity);
    $(ddlState).val(strState);
    $(ddlCountry).val(strCountry);
    $("#overlay .close").click();
    return false;

    txtCitySearch = document.getElementById("<%= txtCity.ClientID %>");
    ddlStateSearch = document.getElementById("<%= ddlState.ClientID %>");
    ddlCountrySearch = document.getElementById("<%= ddlCountry.ClientID %>");
    txtCitySearch.value = strCity;

    ddlStateSearch.selectedIndex = 0

    for (i = 0; i < ddlStateSearch.options.length; i++) {

        if (ddlState.options(i).text.toUpperCase() == strState.toString().toUpperCase()) {
            ddlStateSearch.selectedIndex = i;
            break;
        }
    }

    ddlCountrySearch.selectedIndex = 0;
    for (i = 0; i < ddlCountrySearch.options.length; i++) {
        if (ddlCountrySearch.options(i).text.toUpperCase() == strCountry.toString().toUpperCase()) {
            ddlCountrySearch.selectedIndex = i;
            break;
        }
    }
    $("#overlay .close").click();
    return false;
}

Upvotes: 0

Views: 258

Answers (2)

nnnnnn
nnnnnn

Reputation: 150080

I assume the bit shown at the beginning of your function is your current attempt to use jQuery to do what the second half of your function does? If so, the main problem is just that you haven't set the jQuery selectors up. Try this:

function setDDls(strCity, strState, strCountry) {
   $("#<%= txtCity.ClientID %>").val(strCity);
   $("#<%= ddlState.ClientID %>").val(strState);
   $("#<%= ddlCountry.ClientID %>").val(strCountry);
   $("#overlay .close").click();
   return false;
}

Where in jQuery, as in CSS, you select by id by prefixing the id with "#" such that

$("#<%= txtCity.ClientID %>")

is equivalent to

document.getElementById("<%= txtCity.ClientID %>")

...except that the jQuery version returns a jQuery object regardless of whether there was a match (you can test for no match by checking the .length), while .getElementById() returns a reference to the actual DOM object or null if nothing matched.

If you were having other problems, well, you might want to spell them out in your question.

Upvotes: 0

xdazz
xdazz

Reputation: 160963

Because of the return false, your function is just same with

function setDDls(strCity, strState, strCountry) {
         $(txtCity).val(strCity);
         $(ddlState).val(strState);
         $(ddlCountry).val(strCountry);
         $("#overlay .close").click();
         return false;
}

Upvotes: 1

Related Questions