Showing and hiding div elements

I am looking to enhance this toggle function by making the div content retract and hide when the same link is clicked again. Additionally, I would like it to retract completely when a different link is clicked before the associated content slides down.

Furthermore, I want to learn how to change the color of the link to a different color when its content is visible.

<!DOCTYPE html>
<html lang="en">

<head>
<script type="text/javascript"     
src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<script type="text/javascript">
function slideonlyonee(thechoseone) {
 $('.newframe').each(function(index) {
      if ($(this).attr("id") == thechoseone) {
           $(this).delay(400).slideDown(500);
      }
      else {
           $(this).slideUp(500);
      }
 });
}

</script>

</head>
<body>

<!--COMEDY STARTS HERE-->
<div style="position:relative; top:2px; left:10px;"> 
<a id="myframe" style="text-decoration: none; "   
href="javascript:slideonlyonee('newframe2');"><span style="color:blue;">comedy</span>
</a>

<div class="newframe" id="newframe2" style="background: blue; display:none; block;   
padding: 5px; width: 450px; height:500px; position: relative; left:-5px; top:60px;">
</div>
</div>

<!--DRAMA STARTS HERE-->        
<div style="position:absolute; top:10px; left:144px;"> 
<a id="myframe"  style="text-decoration: none;" 
href="javascript:slideonlyonee('newframe3');"><span style="color:red;">drama</span></a>

<div class="newframe" id="newframe3" style="background:  red; display:none; block;    
padding: 5px; width: 450px; height:500px; position: relative; left:-131px; top:60px;">
</div>
</div>

<!--CRIME STARTS HERE-->
<div style="position:absolute; top:10px; left:283px;"> 
<a id="myframe" style="text-decoration: none;" 
href="javascript:slideonlyonee('newframe4');"><span style="color:green;">crime</a>

<div class="newframe" id="newframe4" style="background:  green; display:none; block; 
padding: 5px; width: 450px; height:500px; position: relative; left:-270px; top:60px;">
</div>
</div>

<!--MAKEOVER STARTS HERE-->
<div style="position:absolute; top:10px; left:407px;"> 
<a id="myframe" style="text-decoration: none;" 
href="javascript:slideonlyonee('newframe5');"><span style="color:purple;">makeover</a>

<div class="newframe" id="newframe5" style="background:  purple; display:none; block;   
padding: 5px; width: 450px; height:500px; position: relative; left:-394px; top:60px;">
</div>
</div>

</body>

</html>

FIND THE FUNCTIONING CODE HERE

I appreciate any assistance in advance!

Answer №1

Here's a suggestion:

$(this).delay(400).slideToggle(500); //<---toggle the slide instead

$('[class="'+thechoseone+'"] span').css({'color':'orange', 'font-weight':'bold'});
//---------------------------^^^^---make sure to include this as well.

All links having the same id is not valid, you should use class instead.

Therefore, the correct HTML links should be:

<a class="myframe"

and for jQuery:

function slideonlyonee(thechoseone) {
    $('.newframe').each(function(index) {
        if ($(this).attr("class") == thechoseone) {
             $(this).delay(400).slideToggle(500);
             $('[class="'+thechoseone+'"] span').css({'color':'orange', 'font-weight':'bold'});
             //---------------------------^^^^---ensuring this is included too.
         }
     });
 }

Answer №2

If you're in need of a solution, this could be what you're searching for:

function revealOnlyOne(divLink, chosenDiv) {

   divLink.css('color','red');
    var selectedDiv = $("#"+chosenDiv);
    $(".content:not(#"+chosenDiv+")").slideUp(500);

    selectedDiv.delay(400).slideToggle(500);
}

The corresponding HTML would look like this:

<a id="myContent" style="text-decoration: none; "   
href="javascript:revealOnlyOne(this,'newContent2');"><span style="color:blue;">entertainment</span>
</a>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why isn't the query working properly to retrieve data from the server in JSON format?

I've encountered an issue where I'm unable to fetch data from my SQL Table using a query. In my controller, I have a GetData ActionResult that is supposed to retrieve data in JSON format from the server, but it's not working. Here's the ...

Method for extracting URL parameters while utilizing a hash within the URL

I've been exploring AJAX with hash in URL using prototypejs. Consider the following URL: http://example.com/#/example/104?v=0&d=a&rpp=10 print_r( $_GET ); // output: array() However, when I try this URL: http://example.com/example/104?v= ...

Tips for retrieving the selected option from a dropdown list using ReactJS

Currently, I am attempting to extract the value of a dropdown menu structured like this: <ul className="tabs" data-component={true}> <li> <section className="sort-list" data-component={true}> <select value={0} class ...

How can I embed an iframe in an Angular 4 application?

I'm facing an issue with my Angular 2 v4 application where I have the following code snippet: <iframe src="https://www.w3schools.com" style="width: 100%; height: 500px"></iframe> However, the iframe does not seem to work. I attempted to ...

Performing an AJAX request using a user-friendly URL

I am currently working on my first website with "pretty URLs", but I am facing an issue with my AJAX Request functions. They are not being directed to the correct file (submit.php). I have a file that contains all the AJAX requests (http://example.com/aja ...

What could be causing my line-clamp to malfunction when using a fixed height in the Safari browser?

I have a problem with the design of my episode list on mobile devices. The list has a fixed height, and I am using the line-clamp-2 property for the episode titles that exceed two lines. While everything looks fine on my PC and even when using Dev Tools t ...

Issue: Having trouble making the frontend work properly due to difficulty in accessing the 'state' property which is undefined

Encountering an issue while attempting to input data from a web application into the database. Having trouble understanding the root cause of the problem. The error occurs when the database insert button is triggered. The following snippets of code showcas ...

Fontawesome is unable to update the class due to the presence of invalid characters in the string

const toggleDarkOrLight = document.getElementsByTagName('i')[0]; var toggled = false; const toggleFunction = () => { if (toggled === false) { toggleDarkOrLight.classList.remove('fa fa-toggle-off'); toggleDarkOrLight.classLi ...

establish a connection with the node server and initiate the automatic mask detection feature

Here's my issue: I have a node server running a game I'm developing. The server listens on an IP address in the network, such as 192.168.x.x (which can vary). The game "frontend" is a web page hosted on a domain (like paperplane.io) that links ...

I am facing an issue with accessing the Controller in Laravel

I am having an issue with the storeClientRequest function not being triggered. When I try to submit, the page displays a 404 Not found error message. Here is my form: <form class="needs-validation" novalidate method="POST" action=&q ...

What is the best way to update the displayed data when using Mobx with an observable array?

Is there a way to re-render observable array data in Mobx? I have used the observer decorator in this class. interface IQuiz { quizProg: TypeQuizProg; qidx: number; state: IStateCtx; actions: IActionsCtx; } @observer class Comp extends Rea ...

What is the best way to ensure that text only displays up to five lines and fully collapses using CSS or javascript/jquery?

Hello everyone, I have come across an issue. English is not my strong suit, but I will do my best to explain it thoroughly. The problem at hand is a design requirement in the draft that specifies showing up to five lines of text. If the text exceeds this ...

parent div with overflowing height

I am working with 2 tabs, each displayed as flex. My goal is to have a scroll bar appear only on the list within #myTabContent if it overflows .main-panel due to the content, rather than having a scroll bar on the entire div. This way, #myTabContent should ...

Image Blob increases over 50 times its original size when uploaded

I'm completely baffled by the situation unfolding here. Using Preprocess.js, I am resizing/compressing an image on the front-end. During the processfile() function on the image.onload (line 32), I convert the toDataURL() string to a Blob, in order to ...

Tips on transmitting checkbox data from AJAX to PHP

I'm currently working with this code and facing an issue where data from checkboxes is being sent to PHP one by one. I want to be able to send multiple selected data at once. How can I modify the code to achieve this? $('#lunas').click(funct ...

The importance of utilizing an object FormData in Node.js to ensure the proper functioning of multer for file uploads via ajax with jQuery

Currently, I am learning about Node.js and using Multer, AJAX, and jQuery for file uploads. However, I am feeling a bit confused. Let's say I have the following code in an HTML file: <form action="/" method='post' enctype=" ...

The default jQuery mobile styles do not automatically apply to list items that are added dynamically

Looking to dynamically generate a list using jquery-mobile. I have the following ul element: <ul data-role="listview" id="bList"></ul> The list items are being added dynamically using this template: <li id="someid" data-wrapperels="div" d ...

Retrieve the content entered in a form text field without needing to officially submit the form

Is it possible to submit the text box value (partnumber) as a query string in a hyperlink without submitting the form itself? I have been trying to achieve this by using document.getElementById("partnumber").value but keep getting an error "Object Required ...

Ways to determine if an object contains an array

Looking at the following data structure: [ 13, { a: [ [ '2988.30000', '0.19000000', '1549294216.653040' ] ] }, { b: [ [ '2988.30000', '0.00000000', '1549294216.653774&a ...

Troubleshooting Test Failures: The importance of passing $controller in the callback of 'it' function in Angular

As a newcomer to testing, I am attempting to write Jasmine/Karma tests for a controller. Given a sample test to use as a starting point, the issue arises when passing the $controller in the argument of the it block. The test passes successfully with this s ...