0

I have 4 cards which contain data. I want when first header text card is clicked, it expand upward, and info I displayed and when clicked the same card it hide the body but the rest card stays the same,

Here is visual of what I want.

enter image description here

UPDATE

Here is JSFiddle with all four cards: http://jsfiddle.net/Mwanitete/rzjxkv8e/2/ Here is HTML:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="data">

<div class="card">
    <div class="card-header">
        <a data-toggle="collapse" href="#test-block" aria-expanded="true" aria-controls="test-block">
            card header
        </a>
    </div>
    <div id="test-block" class="collapse">
        <div class="card-body">
            card block
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <a data-toggle="collapse" href="#test-block" aria-expanded="true" aria-controls="test-block">
            card header
        </a>
    </div>
    <div id="test-block" class="collapse">
        <div class="card-body">
            card block
        </div>
    </div>
</div>
<div class="card">
    <div class="card-header">
        <a data-toggle="collapse" href="#test-block" aria-expanded="true" aria-controls="test-block">
            card header
        </a>
    </div>
    <div id="test-block" class="collapse">
        <div class="card-body">
            card block
        </div>
    </div>
</div>
<div class="card">
    <div class="card-header">
        <a data-toggle="collapse" href="#test-block" aria-expanded="true" aria-controls="test-block">
            card header
        </a>
    </div>
    <div id="test-block" class="collapse">
        <div class="card-body">
            card block
        </div>
    </div>
</div>
</div>

Here is JavaScript

$('.card-header').click(function(){
    $('.card').toggleClass('.card-size')
})

Here is CSS

.data{
  display: flex;
}

.card-size{
  height: 224px;
}
9
  • 2
    What is not working with it? In your fiddle it expands when I click it, but the top of the box is already at the top, so It cannot expand up from what is in your fiddle. Commented Sep 12, 2018 at 15:09
  • First of all, your class to toggle has a point. It shouldn't: toggleClass('card-size'). The point is part of the selector, not of the class. And then, your selector inside the click function selects all .card elements, not those inside the card header selected. Commented Sep 12, 2018 at 15:15
  • 2
    try using $(this).closest('.card').toggleClass('.card-size') Commented Sep 12, 2018 at 15:15
  • Here is how you would do this in CSS, though it is considered a hack. If you can use JS then you should go that route, instead. stackoverflow.com/questions/13630229/… Commented Sep 12, 2018 at 16:25
  • 1
    Using collapse or jQuery slideUp/slideDown obviously don't work, you'll need to write your own CSS animation to make that happen but I've already spent 2+ hours looking at this for you so you'll have to figure that bit out yourself, do a Google search for CSS animations. Commented Sep 12, 2018 at 19:38

3 Answers 3

3

Here's a JSFiddle with a working solution.

I've stripped out the collapse attributes and pulled it down to barebones.

http://jsfiddle.net/9musLxy4/88/

HTML:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="data">

<div class='card-container' data-card-id='1'>
  <div class='card-header' data-card-id='1'>
    Header
  </div>
  <div class='card-body collapse' data-card-id='1'>
    <div class='data'>
      Body 1
    </div>
  </div>
</div>

<div class='card-container' data-card-id='2'>
  <div class='card-header' data-card-id='2'>
    Header
  </div>
  <div class='card-body collapse' data-card-id='2'>
    <div class='data'>
      Body 2
    </div>
  </div>
</div>

<div class='card-container' data-card-id='3'>
  <div class='card-header' data-card-id='3'>
    Header
  </div>
  <div class='card-body collapse' data-card-id='3'>
    <div class='data'>
      Body 3
    </div>
  </div>
</div>

<div class='card-container' data-card-id='4'>
  <div class='card-header' data-card-id='4'>
    Header
  </div>
  <div class='card-body collapse' data-card-id='4'>
    <div class='data'>
      Body 4
    </div>
  </div>
</div>
</div>

CSS:

.data{
  display: flex;
}

.card-body {
  border: 1px solid black;
}

JS:

$('.card-container').on('click', function(event) {
    var id = $(this).data('card-id')
    $('.card-body[data-card-id="' + id + '"]').toggle()
})
Sign up to request clarification or add additional context in comments.

6 Comments

ouch, this would mean you would have to program a click event for each card you create - not very scalable
True, my jquery is so rusty I couldnt - with certainty - suggest how to dynamically add the toggled class on the parent and be confident it is correct. Feel free to expand on the answer.
Thanks to Michael Curry for improving on my answer with the addition of a wildcard selector.
unfortunately now your answer makes no sense as there is no card header with an id - there's a card with an id, or an inner element with a class of card header, but not one with both
Yeah, I've realised that my changes actually just break your stuff. Let me have another crack at it real quick!
|
0
$('.card-header').click(function(){
    $(this).toggleClass('.card-size');
    $(this).parent().find('#test-block').toggleClass('show')
})

Use this

1 Comment

Hi siya, this is not what I want, I have four cards, each card contain data, I want when user click one of the card it should expand upward and display the data, the rest of the card should be the same, a user can click any card, check the visual image for what I want, Thanks for help
0

Are you just trying to change the size of the card in general when it is expanded? If so, lose the JQuery, you just need a CSS style:

#test-block .card-body {
  height: 224px;
}

If you want to the card size to remain the same after you open it, then you would not want to toggle the size class, but simply add it.

$('.card-header').click(function(){
    $(this).closest('.card').addClass('card-size');
})

http://jsfiddle.net/9musLxy4/74/

1 Comment

Hii Esten this is not what I want , here isw clear image of what I need i.sstatic.net/DkHlz.png , jsfidle jsfiddle.net/Mwanitete/rzjxkv8e/6 when a user click one of the card it should exapnd upward

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.