0

I think I've got most of this done, in the best way I know how to anyway. I'm just struggling with the final bit.

I'm trying to generate several quotes at a time, split out by a <br /> tag, depending on what option has been chosen from a dropdown list.

    (function ($) {

        var quotes = ["Quote 1", "Quote 2", "Quote 3", "Quote 4", "Quote 5", "Quote 6", "Quote 7", "Quote 8", "Quote 9", "Quote 10", "Quote 11", "Quote 12", "Quote 13", "Quote 14", "Quote 15"];


        $('.generate').click(function(){
            var randomQuote = quotes[Math.floor(Math.random()*quotes.length)],
                quotesSelected = $( "select option:selected" ).html();

            if (quotesSelected > 1) {
                $('.quote').html(randomQuote + ' -- ' + quotesSelected + ' selected ');
            } else {
                $('.quote').html(randomQuote);
            }
        });

    }(jQuery));

Fiddle: http://jsfiddle.net/mwbhydbu/

So for example -

If 3 is selected, you would be returned with something like Quote 3 <br /> Quote 8 <br /> Quote 1

I hope that makes sense.

This is the particular line I'm struggling with:

$('.quote').html(randomQuote + ' -- ' + quotesSelected + ' selected ');

Any help would be very much appreciated!

2
  • I think you may be saying that you want to add each newly selected quote to the top of the .quote list? Commented Jun 17, 2015 at 21:27
  • @squint if anything greater than 1 is selected I want another random quote to be added. So if 3 is selected, 3 quotes are added split with brs Commented Jun 17, 2015 at 21:30

5 Answers 5

1

Here's how I'd do it:

var quotes = ["Quote 1", "Quote 2", "Quote 3", "Quote 4", "Quote 5", "Quote 6", "Quote 7", "Quote 8", "Quote 9", "Quote 10", "Quote 11", "Quote 12", "Quote 13", "Quote 14", "Quote 15"];

$('.generate').click(function(){                
    var numberToGenerate = $( "select option:selected" ).val();
    var randomQuotes = getRandomQuotes( numberToGenerate );    
    var quoteHtml = randomQuotes.join("<br>");                
    $(".quote").html( quoteHtml );                              
});

function getRandomQuotes( numberOfQuotes ) {
    var results = [];
    if ( numberOfQuotes > quotes.length ) throw new Error("oh dear!");

    while( results.length < numberOfQuotes ) {
        var randomQuote = quotes[Math.floor(Math.random()*quotes.length)];
        if ( -1 === results.indexOf( randomQuote ) ) {
             results.push( randomQuote );
        }
    }                

    return results;
}

Some notes:

  • I use a while loop to keep generating random quotes until I have enough; this would normally be a for loop but I wanted to avoid adding the same quote twice
  • I use array.join() to stick all the quotes together with <br>, and then use the $.html(). In general, this method isn't very safe, because if your quotes contain any actual HTML, then they won't render properly. If you allow user-submitted quotes then this could be an angle of attack to subvert your site.

See http://jsfiddle.net/xvjez408/1/ for a demo.

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

Comments

0

You needed somehting like this

if (quotesSelected > 1) {
                    var txt='';
                    for(var a=1; a< quotesSelected;a++){
                        randomQuote = quotes[Math.floor(Math.random()*quotes.length)],
                    quotesSelected = $( "select option:selected" ).html();
                        txt =txt + randomQuote + ' -- ' + quotesSelected + ' selected </br>';

                    }
                    $('.quote').html(txt);
                } else {
                    $('.quote').html(randomQuote);
                }

have a look http://jsfiddle.net/mwbhydbu/3/ I modified it a little, clean the code :)

Comments

0

Something like this:

(function ($) {

            var quotes = ["Quote 1", "Quote 2", "Quote 3", "Quote 4", "Quote 5", "Quote 6", "Quote 7", "Quote 8", "Quote 9", "Quote 10", "Quote 11", "Quote 12", "Quote 13", "Quote 14", "Quote 15"],
                getRandomQuote = function () {
                    return quotes[Math.floor(Math.random()*quotes.length)]
                };


            $('.generate').click(function(){
                var quotesSelected = $( "select option:selected" ).html(),
                    content = '';
                for (var i = 0; i < quotesSelected; i++) {
                    content = content + '<br />' + getRandomQuote();
                };

                $('.quote').html(content);
            });

        }(jQuery));

Comments

0

You need to call random every time you need a quote.Here is a updated fiddle http://jsfiddle.net/mwbhydbu/4/

if (quotesSelected > 1) {
                    $('.quote').html('');
                    while(quotesSelected>0){
                        randomQuote = quotes[Math.floor(Math.random()*quotes.length)]
                        $('.quote').append(randomQuote+"<br>");
                    quotesSelected--;
                    }
                } else {
                    $('.quote').html(randomQuote);
                }

Comments

0

I think you could just change

quotesSelected = $( "select option:selected" ).html();

to

quotesSelected = $( "select option:selected" ).val();

assuming the values correspond with the indices of their respective title numbers.

My stab at it, comprised entirely of jquery just for fun...

(function($) {

      var quotes = ["Quote 1", "Quote 2", "Quote 3", "Quote 4", "Quote 5", "Quote 6", "Quote 7", "Quote 8", "Quote 9", "Quote 10", "Quote 11", "Quote 12", "Quote 13", "Quote 14", "Quote 15"];

      var select = "<select>";
      for (q in quotes) {
        select += "<option value='" + q + "'>" + quotes[q] + "</option>"
      }
      $('body').append(select + "</select>");
      $('body').append('<p class="quote"></p>');
      $('body').append('<br>');
      $('body').append('<button type="button" class="generate">Generate</button>');

      $('.generate').click(function() {
        var randomQuote = quotes[Math.floor(Math.random() * quotes.length)],
          quotesSelected = $("select option:selected").val();

        if (parseInt(quotesSelected) + 1 > 1) {
          $('.quote').html(randomQuote + ' -- ' + (parseInt(quotesSelected) + 1) + ' selected ');
        } else {
          $('.quote').html(randomQuote);
        }
      });

    }(jQuery));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.