LearnFast.Ninja

How to use jQuery with master pages

The problem: When you using Master Page it is add the Content Place Holder name to your id name.

The problem: When you using Master Page it is add the Content Place Holder name to your id name.
id="ctl00_pageContentPlaceHolder_txtEmail"

The solution:
$("[id$=txtEmail]")

Working example code:

<!DOCTYPE html>

<html>

<head>

    <title>Untitled Page</title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script type="text/javascript">

        function showAlert() {

            var Email = $("[id$=txtEmail]").val();

            alert(Email);

        }

    </script>

</head>

<body>

    <input type="text" id="ctl00_pageContentPlaceHolder_txtEmail" />

    <div onclick="showAlert()" style="color: Blue; cursor: pointer;">

        Show text

    </div>

</body>

</html>


Result:


Tags

jquery

masterpage contentplaceholder asp.net