jQuery text() toggle element text on click

Description

jQuery text() toggle element text on click

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Toggle Text inside Elements</title>
<style>
    button {/*  ww  w .j a v a 2s .  c  om*/
        padding: 5px 10px;
        font-size: 14px;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function(){
        $("button").click(function(){
            $(this).text($(this).text() == 'A' ? 'B' : 'A');
        });
    });
</script>
</head>
<body>
    <button type="button">Order by Alphabet</button>
    <p><strong>Note:</strong> Click on the button to swap the text.</p>
</body>
</html>



PreviousNext

Related