jqueryhtmlsurveyhighest

Get the highest value in div container


I'm very new to this, just trying to piece together snippets from other posts. I have a survey scale which you rate from 1 - 5. After that it will display your answer for each item. Survey results sample

I would like to get the majority of votes.

Sample in my screenshot is 5. Then I will display an equivalent result let's say 1 will display Factor A message and so on ... then 5 will display the message in Factor E. I am having a hard time doing it.

Can you give me an idea?

Here's the code for the output:

<div class="col-sm-3" id="scoreDiv">
<p>#1 - <span class="score" id="score1"></span></p>
<p>#2 - <span class="score" id="score2"> </span></p>
<p>#3 - <span class="score" id="score3"> </span></p>
<p>#4 - <span class="score" id="score4"> </span></p>
<p>#5 - <span class="score" id="score5"> </span></p></div>

Solution

  • try this:

     var _array = [];
     $("#scoreDiv p").each(function(index, el) {
     var text = $(this).text().split(" ");
        text = text[0].split("#");
       _array.push(text[1]);
     });
       alert(Math.max.apply(Math,_array));