How to use HTML Val Method in JQuery

This article describe about HTML Val Method in JQuery.
  • 1927

HTML val() method in jQuery

  • val() method returns sets the html contents of every matched element.

  • val() method is not available on XML documents.

Syntax

$(selector).val()

Example

The following example show the use of val() method.

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function ()

{

        $("button").click(function ()

 {

            alert($("input:text").val());

        });

    });

</script>

</head>

<body>

Firstname: <input type="text" name="fname" value="Hello" /><br />

Lastname: <input type="text" name="lname" value="World" /><br />

<button>Return the value of the first text field</button>

</body>

</html>

Output1

op1.jpg

Output2

op2.jpg

Using a Function

val() method using function to set the value of the value attribute for all selected elements.

Syntax

$(selector).val(function(index,oldvalue))

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.