diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index e56893d3b..35210e4bb 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -2309,6 +2309,56 @@ function test_prop() { var title: string = $('option:selected', this).prop('title'); } +function test_val() { + // Get the value from a dropdown select + $("select.foo option:selected").val(); + + // Get the value from a dropdown select even easier + $("select.foo").val(); + + // Get the value from a checked checkbox + $("input:checkbox:checked").val(); + + // Get the value from a set of radio buttons + $("input:radio[name=bar]:checked").val(); + + function displayVals() { + var singleValues = $("#single").val(); + var multipleValues = $("#multiple").val() || []; + $("p").html("Single: " + singleValues + + " Multiple: " + multipleValues.join(", ")); + } + + $("select").change(displayVals); + displayVals(); + + + $("input") + .keyup(function () { + var value = $(this).val(); + $("p").text(value); + }) + .keyup(); + + $("input:text.items").val(function (index, value) { + return value + " " + this.className; + }); + + $("button").click(function () { + var text = $(this).text(); + $("input").val(text); + }); + + $("input").on("blur", function () { + $(this).val(function (i, val) { + return val.toUpperCase(); + }); + }); + + $("#single").val("Single2"); + $("#multiple").val(["Multiple2", "Multiple3"]); + $("input").val(["check1", "check2", "radio1"]); +} function test_selector() { var $main = $('#main'); diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 4f16a03f9..379368601 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -877,11 +877,28 @@ interface JQuery { */ toggleClass(func: (index: number, className: string, swtch: boolean) => string, swtch?: boolean): JQuery; + /** + * Get the current value of the first element in the set of matched elements. + */ val(): any; - val(value: string[]): JQuery; + /** + * Set the value of each element in the set of matched elements. + * + * @param value A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked. + */ val(value: string): JQuery; - val(value: number): JQuery; - val(func: (index: any, value: any) => any): JQuery; + /** + * Set the value of each element in the set of matched elements. + * + * @param value A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked. + */ + val(value: string[]): JQuery; + /** + * Set the value of each element in the set of matched elements. + * + * @param func A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old value as arguments. + */ + val(func: (index: number, value: any) => any): JQuery; /** * Get the value of style properties for the first element in the set of matched elements.