Recognize Aria-Selected="false" with jQuery

Asked

Viewed 1,179 times

5

How do I make a jQuery that recognizes if aria-selected="false" and if true change the background of the div "arrow-btn-sub" to another image (arrow img). This is part of an acordion and I want to change the image of the arrow when the part is open (<div id="outraDiv">), area Selected appears inside H3.

<h3>
  <a>Banho</a>
  <div class="seta-btn-sub"></div>
</h3>
<div id="outraDiv"> 
  <ul>
    <li><a href="#">Condicionadores</a><span></span></li>
    <li><a href="#">sabonetes</a><span></span></li>
    <li><a href="#">shampoos</a><span></span></li>
    <li><a href="#">outros</a><span></span></li>    
  </ul> 
</div>

I’ve done something like this so far, but I haven’t been able to make it work

<script>
    if($("#aria-selected").val() == true){
        $(".seta-btn-sub").css("background-image","url(images/seta-blue.png);");
    }
    else if($("#aria-selected").val() == false){
        $(".seta-btn-sub").css("background-image","url(images/seta-padrao-azul.png);");
    }
</script>  
  • As far as I understand the aria-selected should be set by a specific implementation, it is not something automatic from HTML or browser. It has some specific plugin that you are using?

  • Yes, I am using "acoordion" (http://jqueryui.com/accordion/) that inserts within H3 the condition (Aria-Selected="true") when I open the desired UL, what I want is to change the image of the arrow (background of the div arrow-btn-sub) that is to the side, to another arrow. Summarizing, i.e., when it is (Aria-Selected="true") change the background of the div "arrow-btn-sub" to another background-image. Thank you

  • @CRAJ Remember that if you want to say more about your problem (like the code example you posted) you don’t need to create a new answer. You can edit your question and post the necessary information. Welcome to SOPT :)

1 answer

6


The .accordion() has two events called beforeActivate and create I used both events to manipulate the header and to remove the icons I used the option icons.

In the beforeActive the headers are ui.newHeader for the element that has just been activated, and ui.oldHeader for the element that has just been deactivated.

In the create the header is the ui.header which will return the active header element as soon as the .accordion() was created.

The result of the code was this:

$(function() {
    $("#accordion").accordion({
        icons: null,
        beforeActivate: function( event, ui ) {
            $("#" + ui.newHeader.attr("id")).children(".seta-btn-sub").toggleClass("active");
            $("#" + ui.oldHeader.attr("id")).children(".seta-btn-sub").toggleClass("active");
        },
        create: function( event, ui ) {
            $("#" + ui.header.attr("id")).children(".seta-btn-sub").toggleClass("active");
        }
    });
});

To make the tests with background I used this :

.seta-btn-sub {
    background: red;
    padding: 10px;
    float:left;
    border-radius: 7px;
}
.seta-btn-sub.active{
    background: #00FF00;
}

That way you can enter this code and put the to display whatever you need.

  • Looking at that answer I think I finally understand the question!

  • this (header) and this (activeHeader) are predetermined or are variables?

  • they are part of the configuration json of .accordion() you can see exactly how it looks here http://jqueryui.com/accordion/#custom-icons

  • But just insert the code into the JS and create the classes with the imgs I want? Why did I do this but nothing happened...

  • You want to put custom images in place of the icons of [tag:jQuery-ui]?

  • Yes, they are 2 different images, similar, but different..

  • Okay, but these images are yours and they’re not the icons of [tag:jquery-ui] that’s it?

  • That’s right, friend, these are two images I developed.

  • Okay, I’ll soon edit my answer to one that fits your need better.

  • Thank you very much, I am waiting because so far I have not found anything effective..

  • I hope now it’s the way you need it

  • Thank you very much Erlon, really stayed as I thought, I will take this structure and study more. Again Thank you!!

  • If you can score as sure I’ll thank you :D

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.