3

I'm trying to change the face value of buttons using an array but what I've got doesn't seem to be working.

$(document).ready(function(){
  var buttonValues = ["The letter 1", "The letter 2", "The letter 3", "The letter 4", "The letter 5"];
  var randomNumber = Math.floor((randomImages.length)*Math.random());
  $(".answer").text(buttonValues[randomNumber]);
});

JQuery above and the HTML below.

<button class="answer" id="1">a</button><br>
<button class="answer" id="2">b</button><br>
<button class="answer" id="3">c</button><br>

Any help would be appreciated, very new to jQuery. I have a couple of other functions going on in the JS file so will post a fiddle of the full code. I may have wrapped something wrong for all I now. Again very new to this. fiddle of current progress

Thanks for the help everyone, realised the silly mistake I made with the code but got some extra advice as well.

2
  • what is `randomImages' ? Commented Apr 25, 2017 at 10:32
  • What is the final output you want? Commented Apr 25, 2017 at 10:32

4 Answers 4

2

You're using a variable which isn't defined:

var randomNumber = Math.floor((randomImages.length)*Math.random());

Should be:

var randomNumber = Math.floor((buttonValues.length)*Math.random());

I'm guessing you'll also want to set different text for each button. Your current approach will set the same text for every button in the collection. Something like this should work:

$('.answer').text(function() {
  var randomNumber = Math.floor((buttonValues.length)*Math.random());
  return buttonValues[randomNumber];
});

Here's an updated fiddle

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

1 Comment

That way you get different button elements with the same text.. Check my answer
1

You are getting error: Uncaught ReferenceError: randomImages is not defined.

I think the variable randomNumber should be the length of the array buttonValues.

Than you should iterate over the buttons with class .abswer and set for jQuery.each() of them the jQuery.text().

Note that inside of the .each() loop you should create the randomNumber in order to get different text for the buttons elements.

And finally, to get all buttons with a different text just remove from the array the element with the randomNumber created index.

Code:

var buttonValues = ['The letter 1', 'The letter 2', 'The letter 3', 'The letter 4', 'The letter 5'];

$('.answer').each(function() {
  var $button = $(this),
      randomNumber = Math.floor(buttonValues.length * Math.random());
  
  $button.text(buttonValues[randomNumber]);
  $button.attr('id', randomNumber);
  
  // Remove element with index randomNumber
  buttonValues.splice(randomNumber, 1);
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<div class="answers">
  <button class="answer" id="1">a</button><br>
  <button class="answer" id="2">b</button><br>
  <button class="answer" id="3">c</button><br>
  <div class="clr"></div>
</div>

4 Comments

Thank you for a very helpful response. Is there any chance you could point me in the right direction for applying the random number so it edits the id tag too? It's fine if you don't want to, you have already helped me immensely.
You mean to set id attribute to randomNumber + 1
Yes this is what I'am after.
Updated the answer: $button.attr('id', randomNumber);
0

I too missed the variable name. Here is modified ans -

var buttonValues = ["The letter 1", "The letter 2", "The letter 3", "The letter 4", "The letter 5"]; var randomNumber = Math.floor((buttonValues.length)*Math.random());

$(".answer").html(buttonValues[randomNumber]);

Comments

-2

Your randomImages array is not defined, code to change button text itself ($(".answer").text(...)) is correct :)

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.