Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Corrects syntax error
Source Link
xr280xr
  • 13.5k
  • 9
  • 88
  • 134

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked


Summary of fiddle:

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked)  //Returns boolean checked state. $("#use").is(":checked")  //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.


See jQuery's API documentation on .prop for more info about this.

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked


Summary of fiddle:

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked) //Returns boolean checked state. $("#use").is(":checked") //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.


See jQuery's API documentation on .prop for more info about this.

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked


Summary of fiddle:

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked  //Returns boolean checked state. $("#use").is(":checked")  //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.


See jQuery's API documentation on .prop for more info about this.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this threadthis thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked


Summary of fiddle:

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked) //Returns boolean checked state. $("#use").is(":checked") //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.


See jQuery's API documentation on .prop for more info about this.

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked


Summary of fiddle:

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked) //Returns boolean checked state. $("#use").is(":checked") //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.


See jQuery's API documentation on .prop for more info about this.

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked


Summary of fiddle:

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked) //Returns boolean checked state. $("#use").is(":checked") //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.


See jQuery's API documentation on .prop for more info about this.

Formatting
Source Link
xr280xr
  • 13.5k
  • 9
  • 88
  • 134

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked


Summary of fiddle:

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked) //Returns boolean checked state. $("#use").is(":checked") //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.

 

See jQuery's API documentation on .prop for more info on checkboxesabout this.

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked) //Returns boolean checked state. $("#use").is(":checked") //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.

See jQuery's API documentation on .prop for more info on checkboxes.

Like Simon said, .val() returns the value attribute of the checkbox, which can be any value you want. But more specifically, it returns the value property of the DOM object, which is initialized by the attribute. MVC seems to set the value to "true" by default, which seems pretty meaningless to me. But in some cases you might want want to set it to an ID or some other meaningful value for the checked option as this thread points out.

Nicholas provided one good way of getting whether the checkbox is checked above. Check and uncheck the checkbox on this jsfiddle for an example of the various approaches and their results:

JQuery Checkbox Checked


Summary of fiddle:

$("#use").val(); //Returns the value. Not related to checked state. $("#use").attr("checked") //Returns the checked attribute value. Does not change with checked state. $("#use").prop("checked") //Returns boolean checked state. $("#use")[0].checked) //Returns boolean checked state. $("#use").is(":checked") //Returns boolean checked state. 

Notice the results of .val and .attr do not change. That is because the val property is not dependent on the checked state, and the actual checked attribute in the markup does not update when the checkbox changes. Only the property on the DOM object behind the scenes, which is what the last three methods access.

 

See jQuery's API documentation on .prop for more info about this.

Source Link
xr280xr
  • 13.5k
  • 9
  • 88
  • 134
Loading