Is there a way to modify the list-style-image while using slideToggle?

Html

<ul class="main">
    <li class="item_no">Item 1
        <ul class="sub_items">
            <li>Item 1.1</li>
            <li>Item 1.2</li>
            <li>Item 1.3</li>
        </ul>
    </li>
    <li class="item_no">Item 2
        <ul class="sub_items">
            <li>Item 2.1</li>
            <li>Item 2.2</li>
            <li>Item 2.3</li>
        </ul>
    </li>
    <li class="item_no">Item 3</li>
</ul>

css

ul.main {
    list-style-type:none;
}
ul.main li.item_no {
    list-style-image:url('arrow.png');
}
ul.main ul.sub_items li {
    list-style-image:none;
}

script

$(document).ready(function() {
    $('ul.sub_items').hide().filter(':lt(1)').show();

    $("li.item_no").click(function() {
        $(this).find(".sub_items").slideToggle("slow");
    });
});

If the "item_no" is clicked, I want to switch the list-style-image from "arrow.png" to "arrowdown.png".

Does anyone have a solution for this request?

Answer №1

Here is a helpful solution:

$(document).ready(function () {
    $('.sub_items').hide().first().show();
    $(".main > li").click(function () {
        var self = $(this);
        self.toggleClass('item_no item_down');
        self.children('.sub_items').slideToggle('slow');
    });
});

Don't forget to include this CSS:

ul.main li.item_down{
     list-style-image:url('arrowdown.png');
}

Check out the Working Fiddle here

Answer №2

Check out this interactive demonstration

$("#ul_lst").hide();
$("#img_up").hide();
$("#arrow_up").hide();
$("#link1").click(function () {

$(".exclass").slideToggle(1000);
$("#arrow_up").toggle();
$("#arrow_dw").toggle();
$("#img_up").toggle();
$("#img_down").toggle();
 
});
.demomenu {
width: 220px;
padding: 0 0 0 0;
margin-bottom: 1em;
font-weight: normal;
font-family: Verdana, Lucida, Geneva, Helvetica, Arial, sans-serif;
color: #333;
}
.linkclass {
display: inline-block;
padding: 3px 0px 3px 0.5em;
border-left: 5px solid #8AA1B6;
border-right: 5px solid #8AA1B6;
background-color: #6898d0;
color: #fff;
text-decoration: none;
width: auto;
}
.exclass
{
width: 220px;
padding: 0 0 0 0;
font-size: 11px;
font-weight: normal;
font-family: Verdana, Lucida, Geneva, Helvetica, Arial, sans-serif;
background-color: #e5eecc;
color: 000000; 
}
.lst_sty{
list-style:none;
}
.img_up
{
background:url('http://i.imgur.com/fpwRSGA.png');
display:inline-block;
}
.img_down
{
background:url('http://i.imgur.com/WmtRYL9.png');
display:inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul class="exclass" id="ul_lst"><li class="lst_sty">This is sliding text example! This is sliding text example! </li>
<li class="lst_sty">This is sliding text example! This is sliding text example! </li>
<li class="lst_sty">This is sliding text example! This is sliding text example! This is sliding text example! </li>
<li class="lst_sty">This is sliding text example!</li></ul>
<div class="demomenu">
<a class="linkclass" id="link1" href="#">Products </a><span class="arrow_up" id="arrow_up">></span><span class="arrow_dw" id="arrow_dw"><</span>
    <img class="img_up" id="img_up" height="24" width="24" >
<img class="img_down" id="img_down" height="24" width="24" > 
</div>

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

It appears that the $http request is causing an endless $digest Loop

Looking to determine a user's status in my AngularJS app in order to display specific functionality content, here is the HTML code in my view: <span ng-show="authService.isSuperUser()">You are a Super User</span> To check if the user has ...

Perform a search using indexOf method on JSON.stringify objects

I am attempting to determine whether a specific string exists in the following code: var test1 = '{"packageId":"1","machineId":"1","operationType":"Download"},{"packageId":"2","machineId":"2","operationType":"Download"}'; alert("found: " + test1 ...

What is the proper way to include a URL when submitting an AJAX form?

When I try to call a servlet using HTML, jQuery and Ajax by submitting a form, I keep getting a 404 error saying the resource cannot be found. My project is built with Maven. The Servlet is located in the src/main/java package under : com.mycompany.sample ...

Searching recursively for keys with empty values in an array using Javascript

I've written a function that recursively locates empty values in a nested array. The function initially produces the correct value, but it seems to reset it to the input value before returning the result. What could I be overlooking? Below is my co ...

What are the most effective methods for organizing a substantial redux project?

After recently diving into Redux, I've been enjoying the process so far. However, as my application continues to expand, I find myself questioning the best practices for structuring it. The tutorials and documentation provide small examples, leaving m ...

What is the best way to remove an object from a complex JavaScript array structure?

I'm having trouble removing an object from the array provided below. Whenever I try to delete its properties, they just turn into undefined. I have a recursive function that correctly identifies the object I want to remove. Specifically, I need to eli ...

Receiving an error when trying to access the 'get' property of $http within a nested function

Encountering the following error angular.js:13550 TypeError: Cannot read property 'get' of undefined at Object.DataService.getDataFromAPI I seem to be struggling with the $HTTP.get function. Any insights on what might be going wrong or ...

Is there a way to temporarily toggle classes with jQuery?

Incorporating ZeroClipboard, I have implemented the following code to alter the text and class of my 'copy to clipboard button' by modifying the innerHTML. Upon clicking, this triggers a smooth class transition animation. client.on( "complete", ...

Passing a Boolean value within a component invocation from the index.html file

I have a scenario where I am working on a component and trying to pass a boolean value inside it as an @input. This boolean value should help in showing or hiding a div. However, I am encountering an issue where it is returning undefined. Can someone pleas ...

The request to http://localhost:3306/socket.io/?EIO=3&transport=polling&t=NQfLxAy encountered an invalid HTTP response, resulting in net::ERR

I'm currently working on a project to build a real-time chatbox using Node.js. Right now, I am facing an issue where I am unable to display the user's entered name in the console. I would greatly appreciate any help with resolving this problem. ...

The function .text() within the JSON success callback is causing repetitive data to be

Situation: I recently integrated JSON data from SmartRecruiters API into my layout, specifically focusing on displaying job descriptions. Due to some job descriptions being lengthy, I decided to limit the characters displayed within each description to a s ...

Ways to implement jQuery for tables in a Ruby on Rails environment

I am a beginner in web app development with RoR and I could use some assistance with jQuery. On my webpage, I have a table displaying messages along with two moderation buttons - Accept and Decline message. .../view/messages/index.html.erb <div class ...

When trying to validate an HTML form using AJAX, jQuery, and JavaScript, the validation may not

Here is a high-level overview of what happens: The following code functions correctly... <div id='showme'></div> <div id='theform'> <form ...> <input required ... <input required ... <inpu ...

What is the best way to evenly distribute navigation li tags within a specific width?

I'm struggling to evenly space out my navigation bar across the 4 column grid. I want each list item to function more like a button block and have equal widths so that the hover-on border tops are all symmetrical. Check out the website: HTML: <d ...

Send JSON information to the server and interpret the feedback with JavaScript

Can someone please provide guidance on how to send JSON format data via POST to a server URL, receive the response in JSON format, parse it, and retrieve the data? An example would be greatly appreciated. Thank you. ...

Struggling to access a nested object within JSON using dot notation

Currently, I am utilizing mongoose and express to interact with data stored in MongoDB. I am able to view the JSON data when I reference the database. However, I encounter an issue when attempting to specifically target an object nested within the data, as ...

Creating a Node.js application using Express requires careful consideration of file structure and communication methods

Struggling to pass login form credentials to existing JavaScript files for logic and info retrieval. Login page contains a form with POST method. main.js handles the login: main.js module.exports = { returnSessionToken: function(success, error) { ...

Technique for seamlessly moving between subpages and main page while scrolling to a specific id

I'm struggling with coding and thought I'd reach out for help here. Can anyone provide a solution to my problem? Issue - I have a navigation link on my main page that scrolls to an ID using jQuery. It works fine on the main page, but not on any ...

Error encountered when trying to dynamically load jQuery UI - Uncaught TypeError: Unable to read property 'ui' of undefined

I am facing an issue with dynamically loading my jQuery and jQuery UI files. The jQuery file loads successfully, but an error occurs when the jQuery UI file attempts to load. Upon checking the console, I found the following error message: Uncaught TypeErr ...

How can you position an absolute DIV with an unknown height outside of a "relative" parent using CSS?

Imagine a straightforward illustration using html: <div class="parent"> <div class="child"> </div> </div> and CSS: .parent{ position:relative; background:red; width:200px; height:40px; } .child{ positi ...