1

With this html:

<div class="outer">
    <div class="inner">
        <span class="selection" title="Green"></span>
    </div>
    <div class="inner">
        <span class="selection" title="Yellow"></span>
    </div>
    <div class="inner">
        <span class="selection" title="Red"></span>
    </div>
</div>

and this existing jQuery which can be changed as necessary:

$(document).ready(function(){
    $('.outer .inner span.selection').each(function(){
        if($(this).attr('title', 'Green')) {
            $(this).parent().css('background-color', 'green');
        }
        if($(this).attr('title', 'Yellow')) {
            $(this).parent().css('background-color', 'yellow');
        }
        if($(this).attr('title', 'Red')) {
            $(this).parent().css('background-color', 'red');
        }
    });
});

Essentially this only displays a single item in a dropdown. I'm looking to change the list item displayed as a single color. Other list items that aren't selected are hidden.

1
  • your question is unclear, please could you clarify? Commented Jan 19, 2016 at 15:36

2 Answers 2

2

You are setting the value to the title and not comparing, try this:

$(document).ready(function() {
  $('.outer .inner span.selection').each(function() {
    var title = $(this).attr('title');
    console.log(title);
    if (title === 'Green') {
      $(this).parent().css('background-color', 'green');
    }
    if (title === 'Yellow') {
      $(this).parent().css('background-color', 'yellow');
    }
    if (title === 'Red') {
      $(this).parent().css('background-color', 'red');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="outer">
  <div class="inner">
    <span class="selection" title="Green">Green</span>
  </div>
  <div class="inner">
    <span class="selection" title="Yellow">Yellow</span>
  </div>
  <div class="inner">
    <span class="selection" title="Red">Red</span>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

Excellent! I'll try it out. Thanks for the fast reply.
This working when all of the div elements are displayed, but not when they are hidden and selected from the dropdown list. I'm modifying it to try and figure out the problem.
Yay, you stored the attribute value in a variable!
@MarcusFaye that is another dependency, for this issue ask another question, multiple question where only is supposed to be are always bad. This code answer solves your issue as you didn't give more enviormental variables, i suppose.
Here's the more specific expansion of this question, certain variables are adjusted to be more specific stackoverflow.com/questions/34882354/…
1

Hi im not exactly sure what you mean with your question. But are you trying to get individual color put as the background for the sections, instead of just the last color that it checks for?

You need to set the value to what equals the title instead of comparing it. I fixed it in this code pen: http://codepen.io/anon/pen/eJeKgZ

$(document).ready(function(){
    $('.outer .inner span.selection').each(function(){
        if($(this).attr('title') == 'green') {
            $(this).parent().css('background-color', 'green');
        }
        if($(this).attr('title') == 'yellow') {
            $(this).parent().css('background-color', 'yellow');
        }
        if($(this).attr('title') == 'red') {
            $(this).parent().css('background-color', 'red');
        }
    });
});

Comments

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.