You are developing a customer web form that includes the following HTML.
<label id=”txtValue” </label>
Information from the web form is submitted to a webservice.
The web service returns the following JSON object.
{
“Confirmation”: “1234”,
“FirstName”: “John”
}
You need to display the Confirmation numberfrom the JSON response in the txtValue label field.
Which JavaScript code segment should you use?
A.
$(“#txtValue”).val = (JSONObject.Confirmation);
B.
$(“#txtValue”).val(JSONObject.Confirmation);
C.
$(“#txtValue”).text = (JSONObject.Confirmation);
D.
$(“#txtValue”).text(JSONObject.Confirmation);
Explanation:
.val()
Get the current value of the first element in the set of matched elements or set the value of every matched
element.
.text()
Get the combined text contents of each element in the set of matched elements, including their descendants,
or set the text contents of the matched elements.
i think is val()
val(), is the best way to get/set value for input , select etc…
From the jQuery API Documentation (http://api.jquery.com/text/):
“The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.”
oops, ”txtValue” is the label element, so you can to use .text() method.
Tested it and text() is correct for label.
hi Ivan check the page further for version 1.0 and 1.4 , now text can take parameter to get the assigned value