Saturday, 19 August 2017

jQuery: Applying attribute based css

In this tutorial I am going to make sample that will help you to understand that how you can apply css on html via attributes. 


Step 1: Add following html code in your html file.
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <div id="divOuter" state="loaded">
        <div id="divError">
            This is error section
        </div>
        <div id="divSuccess">
            This is success section
        </div>
    </div>
    <div>
        <button id="btnError">Show error</button>
        <button id="btnSuccess">Show Success</button>
    </div>
</body>
</html>

Step 2: Add following js code:
<script type="text/javascript">
    $("#btnError").click(function () {
        $("#divOuter").attr("state", "error");
    });
    $("#btnSuccess").click(function () {
        $("#divOuter").attr("state", "loaded");
    });
</script>

Step 3: Add following css in style tag
<style>
    #divOuter[state="loaded"] #divSuccess, #divOuter[state="error"] #divError {
        display: block;
    }

    #divOuter[state="loaded"] #divError, #divOuter[state="error"] #divSuccess {
        display: none;
    }
</style>

No comments:

Post a Comment