Bruno Sundelin
Bruno Sundelin

Reputation: 11

Target specific class in div for javascript

I would like to target only the classes "Front" and "Back" of the "Flip-tile" that I am currently hovering over. How do you solve this with javascript, currently it is toggling them all at the same time.

The HTML:

    <div class="flip-tile">
        <div class="front">
            <div class="project-tile primary-color col-md-4 col-xs-12">
                <img class="tile-img" src="../static/img/first.png">
                <div class="tile-text">
                    <h4><span class="light">Front side</span></h4>
                </div>
            </div>
        </div>
        <div class="back" style="display:none;">
            <div class="project-tile secondary-color col-md-4 col-xs-12">
                <div class="tile-text">
                    <h4><span class="light">Back side</span></h4>
                </div>
            </div>
        </div>
    </div>

    <div class="flip-tile">
        <div class="front">
            <div class="project-tile primary-color col-md-4 col-xs-12">
                <img class="tile-img" src="../static/img/first.png">
                <div class="tile-text">
                    <h4><span class="light">Front side</span></h4>
                </div>
            </div>
        </div>
        <div class="back" style="display:none;">
            <div class="project-tile secondary-color col-md-4 col-xs-12">
                <div class="tile-text">
                    <h4><span class="light">Backside</span></h4>
                </div>
            </div>
        </div>
    </div>

The Javascript:

$(".flip-tile").hover(function() {
    $(".front").hide()
    $(".back").show()},
    function() {
    $(".back").hide()
    $(".front").show()
});

Upvotes: 1

Views: 65

Answers (4)

fbsanchez
fbsanchez

Reputation: 9

Please take a look on https://api.jquery.com/class-selector/

I.e.

<script>
$( ".myclass.otherclass" ).css( "border", "13px solid red" );
</script>

The example will apply the border style to any .myclass.otherclass items along the HTML

good luck!

Upvotes: 1

Mr. Roshan
Mr. Roshan

Reputation: 1805

Try this:-

$(".flip-tile").hover(function() {
     $(this).find(".front").hide()
     $(this).find(".back").show()},
    function() {
      $(this).find(".back").hide()
     $(this).find(".front").show()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="flip-tile">
        <div class="front">
            <div class="project-tile primary-color col-md-4 col-xs-12">
                <img class="tile-img" src="../static/img/first.png">
                <div class="tile-text">
                    <h4><span class="light">Front side</span></h4>
                </div>
            </div>
        </div>
        <div class="back" style="display:none;">
            <div class="project-tile secondary-color col-md-4 col-xs-12">
                <div class="tile-text">
                    <h4><span class="light">Back side</span></h4>
                </div>
            </div>
        </div>
    </div>

    <div class="flip-tile">
        <div class="front">
            <div class="project-tile primary-color col-md-4 col-xs-12">
                <img class="tile-img" src="../static/img/first.png">
                <div class="tile-text">
                    <h4><span class="light">Front side</span></h4>
                </div>
            </div>
        </div>
        <div class="back" style="display:none;">
            <div class="project-tile secondary-color col-md-4 col-xs-12">
                <div class="tile-text">
                    <h4><span class="light">Backside</span></h4>
                </div>
            </div>
        </div>
    </div>

Upvotes: 0

Marcos P&#233;rez Gude
Marcos P&#233;rez Gude

Reputation: 22158

It's so easy using find() on jQuery, but you can make it on easiest way with CSS.

Javascript:

$(".flip-tile").hover(function() {
    $(this).find(".front").hide();
    $(this).find(".back").show();
  },
  function() {
    $(this).find(".back").hide();
    $(this).find(".front").show();
  }
);

CSS solution

.flip-title .front {
  display: block;
}
.flip-title .back {
  display: none;
}
.flip-title:hover .front {
  display: none;
}
.flip-title:hover .back {
  display: block;
}

Upvotes: 2

Programmer
Programmer

Reputation: 2123

Use $(this).find(...).

$(".flip-tile").hover(function() {
    $(this).find(".front").hide()
    $(this).find(".back").show()},
    function() {
    $(this).find(".back").hide()
    $(this).find(".front").show()
});

Upvotes: 1

Related Questions