0

Ok so I have this code that is able to select and count the amount of select boxes that have a specific option selected. However, I want to change it to groups. I have 3 groups of drop downs, and they are currently in

<div class"groupA"> 

etc.

What I want to do is get the counts from each group. how can I modify this script to count selected only within a certain group on the page? right now it works but counts how many are selected on the entire page.

<script type="text/javascript">


function getSelectedValues(){
  var matches = $('select').filter(function(){
  return $(this).val() == '5';
  }).length;
  // here's where we just update that new <span>
  $('span#result5').text(matches);
}

// and here we bind to the change event for the selects
// and re-call our above function.
$('select').on('change', getSelectedValues);

   </script> 


 <span id="result5"></span>

Second, right now that span id=result displays the amount of times that specific option was selected. instead, I want it to change text color to red if the amount counted is over 1. so here is my poor example of what I want it to do:

<span id="result5"></span>

This select name <-- how it displays when 0, or 1 are selected

<font color="#FF0000">This select name</font>
    ^-- how it displays when 2 or more are selected
ant t
  • 25
  • 8

1 Answers1

1

to select only 'selects' in your group try to select them with:

var matches = $('.groupA select')...

to change it to red try following:

// here's where we just update that new <span>
if(matches > 1){
    $('span#result5').css( "color", "red" );
}
$('span#result5').text(matches);

i hope i could help

Joniras
  • 1,258
  • 10
  • 32
  • for the second part, wouldnt that still return the value of amount selected? i wish not to do that, but instead turn the option to be selected red. In otherwords, i dont want them to accidentally select the same item for more than one question. so turning it red in the next drop down says "you already selected this answer on another question" – ant t Feb 24 '15 at 16:39
  • tried this: [link](http://stackoverflow.com/questions/15755770/change-text-color-of-selected-option-in-a-select-box) ? And to get your selected option add an event object to your function to evaluate the sender: function `getSelectedValues(eventobject)...` [event object](http://api.jquery.com/category/events/event-object/) – Joniras Feb 24 '15 at 16:42
  • $('.groupA select') didnt work. i tried $('groupA select') as well, still not grabbing it. it is displaying a 0 tho so it is counting just not what its supposed to be. i have div id="groupA" – ant t Feb 24 '15 at 16:57