Difference Between Val() and Text()

Difference between val() and text()

.val() works on input elements (or any element with a value attribute?) and .text() will not work on input elements. .val() gets the value of the input element -- regardless of type. .text() gets the innerText (not HTML) of all the matched elements:

.text()

The result is a string that contains
the combined text contents of all
matched elements. This method works on
both HTML and XML documents. Cannot be
used on input elements. For input
field text use the val attribute.

.val()

Get the content of the value attribute
of the first matched element

What is the difference between Val(input.Text) and input.Text in VB?

Val (Here the MSDN reference) is an function still present in VB.NET to maintain some kind of compatibility with VB6.

It was used to obtain the numeric value of a string containing numbers characters.

There is no sense in applying the Val function to a TextBox containing a name.

However, the Val function, if cannot convert the passed string to a number, returns zero, and thus you get the error message saying the two values are different.

And, if you don't get an error message when trying to compare the result of Val (a double) with a string, then you have your Option Strict set to Off. A configuration that you should try to avoid at all costs (in new applications at least)

if Val(Name1) = "Zoe" Then

' it is equal to '
if 0# = "Zoe" Then

and this should be a compilation error.

When do I use .val() vs .innerHTML?

.val() is used to get/replace input elements values in jQuery, alternative in JS is .value.

innerHTML or jQuery's .html() is used to get/replace the whole markup inside an element, not input elements.

text() is used almost the same as JS innertHTML, only it gets/replaces the text inside an element, not all the tags etc. It's bassically the equivalent of JS innerText

Reference links about innerHTML, innerText, val(), text(), html()

val() vs. text() for textarea

The best way to set/get the value of a textarea is the .val(), .value method.

.text() internally uses the .textContent (or .innerText for IE) method to get the contents of a <textarea>. The following test cases illustrate how text() and .val() relate to each other:

var t = '<textarea>';
console.log($(t).text('test').val()); // Prints test
console.log($(t).val('too').text('test').val()); // Prints too
console.log($(t).val('too').text()); // Prints nothing
console.log($(t).text('test').val('too').val()); // Prints too

console.log($(t).text('test').val('too').text()); // Prints test

The value property, used by .val() always shows the current visible value, whereas text()'s return value can be wrong.

What's the difference between jQuery .val() and .attr('value')?

.val() works on all input type elements in a useful way, including <select>...even in the cases of <select multiple>, checkboxes, and radio buttons (in which .val() gets or sets an array of selected values not just a string).

So basically they serve different purposes, even though .attr('value') behaves the same in some situations, like textboxes. The preferred method is .val() to get consistent behavior everywhere.


Just for kicks, here's a lesser-known example for checkboxes that makes .val() handy:

<input name="mytest" type="checkbox" value="1">
<input name="mytest" type="checkbox" value="2">
<input name="mytest" type="checkbox" value="3">
<input name="mytest" type="checkbox" value="4">

You can do this:

$("input[name='mytest']").val([1, 2, 3]);

....which will check the first 3 boxes. You can give it a try here.

Difference between $this.text and $this.value and why 1 returns a null value?

.text() gets or sets the text inside an element.

For example: <p>Hello World</p>

console.log($('p').text()) will return Hello World

.value() isn't a jquery method, instead use .val()

.val() is used to get or set the value of an input field.

For example <input type="text"> If you wanted to get or set the value of the input, say on form submit, you would use something like $('input').val()

.val() or .text() to find value of input in form?

If you pass an argument into text(), you are setting the content of td.quantity to that string, and JQuery will return the object for chaining. If you call text() with no argument, you will get the text inside the element.

You should use .val() to retrieve the values of input/textarea/selects elements, like so:

var BSKfirstQTY = $(BSKfirstrow).find('td.quantity input').val();

For more info on val vs. text, see this question.

What is the difference between Val(input.Text) and input.Text in VB?

Val (Here the MSDN reference) is an function still present in VB.NET to maintain some kind of compatibility with VB6.

It was used to obtain the numeric value of a string containing numbers characters.

There is no sense in applying the Val function to a TextBox containing a name.

However, the Val function, if cannot convert the passed string to a number, returns zero, and thus you get the error message saying the two values are different.

And, if you don't get an error message when trying to compare the result of Val (a double) with a string, then you have your Option Strict set to Off. A configuration that you should try to avoid at all costs (in new applications at least)

if Val(Name1) = "Zoe" Then

' it is equal to '
if 0# = "Zoe" Then

and this should be a compilation error.



Related Topics



Leave a reply



Submit