LearnFast.Ninja

jQuery find class inside div

In this example we change the CSS of the class inside the div. We do not use just space and span select because we want to change only the A char and not all A B C.

Main idea:
$('#MyDiv > .MyClass').css({ 'color''red' });

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>

</head>

<body>

    <div id="MyDiv">

        <span class="MyClass">A</span>

        <span>B</span>

        <span>C</span>

    </div>

    <br />

    <div onclick="$('#MyDiv > .MyClass').css({'color': 'red'});">

        Click here to change the text color to MyClass

    </div>

</body>

</html>


Result:


Tags

jquery

css