0

I have a problem where I didn't find the answer for a few days, besides I was new to JavaScript.

$(document).on('click', '.content-click', function(){
    $(".content-click span").toggleClass("clicked"),
    $(".content-view").toggleClass("viewed");
    $(this).show();
});
.content-click {
    display: block;
    width: 100%;
    height: 2rem;
    padding: 0.375rem 0.75rem;
    font-size: .75rem;
    font-weight: 500;
    line-height: 1.5;
    color: #495057;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #ced4da;
    border-radius: 0.25rem;
    cursor: pointer;
}

.content-click p {
    display: inline;
    margin: 0;
}

.content-click span {
    width: 10px;
    height: 10px;
    position: relative;
    top: 5px;
    float: right;
    vertical-align: middle;
    background: url(../img/arrow.png) no-repeat;
    transition: all 0.3s ease;
    transform: rotate(0deg);
}

.content-click span.clicked {
    transform: rotate(90deg);
} /*button click styling */

.content-view {
    display: block;
    width: 100%;
    height: 0;
    border: 0px solid #ebebeb;
    box-sizing: border-box;
    margin-top: 0rem;
    margin-bottom: 0rem;
    position: relative;
    border-radius: .25rem;
    padding: 0;
    font-size: 0;
    font-weight: 500;
    opacity: 0;
    transition: all 0.2s ease;
}

.content-view::after {
    content: '';
    width: 10px;
    height: 10px;
    border-top: 1px solid #ebebeb;
    border-right: 0px solid #ebebeb;
    border-bottom: 0px solid #ebebeb;
    border-left: 1px solid #ebebeb;
    position: absolute;
    left: 95%;
    top: 0%;
    margin-top: -6px;
    margin-left: -6px;
    transform: rotate(45deg);
    background-color: #fff;
}

.content-view.viewed {
    height: auto;
    border: 1px solid #ebebeb;
    margin-top: .25rem;
    margin-bottom: 1rem;
    font-size: .75rem;
    padding: 1rem;
    opacity: 1;
} /*description area-text styling*/
<div class="container">
            <div class="content-click" style="margin:.25rem;">
                <div id="content-1">
                    <p>First Item list...</p>
                    <span></span>
                </div>
            </div>
            <div class="content-view">
                <div id="view-1">
                    <p>Description...</p>
                </div>
            </div>
            <div class="content-click" style="margin:.25rem;">
                <div id="content-2">
                    <p>First Item list...</p>
                    <span></span>
                </div>
            </div>
            <div class="content-view">
                <div id="view-2">
                    <p>Description...</p>
                </div>
            </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
In that case, i created buttons, which if any user click on it, the text area or description window will pop up. But in this case, i have only 2 items, which so easy to maintain, but later on, how if i have 100 items? it means i need 100 id's for each items right?

and once I add the class name as the .toggleClass target, the same items which have the exact same class will pop the description, even i only click on the first button. sorry for asking dumb question.

Raden Kriting
  • 311
  • 1
  • 2
  • 14

2 Answers2

0

You need to maintain the scope of your click event, so that whenever you click an item, only the next description expands, not all of them.

Here is the code:

$(document).on('click', '.content-click', function(){
    $(this).find("span").toggleClass("clicked"); // this finds the child element 'span'
    $(this).next().toggleClass("viewed"); // .next() is the selector for the next sibling element;
    $(this).show();
});
.content-click {
    display: block;
    width: 100%;
    height: 2rem;
    padding: 0.375rem 0.75rem;
    font-size: .75rem;
    font-weight: 500;
    line-height: 1.5;
    color: #495057;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #ced4da;
    border-radius: 0.25rem;
    cursor: pointer;
}

.content-click p {
    display: inline;
    margin: 0;
}

.content-click span {
    width: 10px;
    height: 10px;
    position: relative;
    top: 5px;
    float: right;
    vertical-align: middle;
    background: url(../img/arrow.png) no-repeat;
    transition: all 0.3s ease;
    transform: rotate(0deg);
}

.content-click span.clicked {
    transform: rotate(90deg);
} /*button click styling */

.content-view {
    display: block;
    width: 100%;
    height: 0;
    border: 0px solid #ebebeb;
    box-sizing: border-box;
    margin-top: 0rem;
    margin-bottom: 0rem;
    position: relative;
    border-radius: .25rem;
    padding: 0;
    font-size: 0;
    font-weight: 500;
    opacity: 0;
    transition: all 0.2s ease;
}

.content-view::after {
    content: '';
    width: 10px;
    height: 10px;
    border-top: 1px solid #ebebeb;
    border-right: 0px solid #ebebeb;
    border-bottom: 0px solid #ebebeb;
    border-left: 1px solid #ebebeb;
    position: absolute;
    left: 95%;
    top: 0%;
    margin-top: -6px;
    margin-left: -6px;
    transform: rotate(45deg);
    background-color: #fff;
}

.content-view.viewed {
    height: auto;
    border: 1px solid #ebebeb;
    margin-top: .25rem;
    margin-bottom: 1rem;
    font-size: .75rem;
    padding: 1rem;
    opacity: 1;
} /*description area-text styling*/
<div class="container">
            <div class="content-click" style="margin:.25rem;">
                <div id="content-1">
                    <p>First Item list...</p>
                    <span></span>
                </div>
            </div>
            <div class="content-view">
                <div id="view-1">
                    <p>Description...</p>
                </div>
            </div>
            <div class="content-click" style="margin:.25rem;">
                <div id="content-2">
                    <p>First Item list...</p>
                    <span></span>
                </div>
            </div>
            <div class="content-view">
                <div id="view-2">
                    <p>Description...</p>
                </div>
            </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
Towkir
  • 3,889
  • 2
  • 22
  • 41
0

Don't use ids to find elements when you have a relationship like this.

First, capture the event object.

$(document).on('click', '.content-click', function(event){

Then use it to get the clicked element

var $clicked = $(event.currentTarget);

… and from there use functions like parent and find to navigate to the associated element you want to deal with.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335