0

I want the user when he/she clicks a button for a value from an array to be inserted into one of five div boxes. The code has no errors on firebug but when I click the button nothing happens. What's wrong with my code. Here is the jsfiddle example http://jsfiddle.net/fHHnq/13/

Here is the relevant js code...

var news = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
        "12", "13", "14", "15", "16", "17", "18", "19", "20"];


$(document).ready(function() {

 $('.button').click(function() {

    var article_id = 1;

    var number = Math.floor(Math.random() * 5) + 1;

    $('.class_' + number).html('news[article_id]');

    article_id++;

 });

});
1
  • once fix hyphens above code won't work because of quotes around variable: news[article_id] Commented Nov 23, 2012 at 1:50

2 Answers 2

3

You're incorrectly assigning classes and your HTML uses -, but your JavaScript is using _ for class names.

Here is the working jsfiddle: http://jsfiddle.net/fHHnq/16/

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

Comments

0

In your html you are using " class-1 " and in your javascript you are using " $('.class_' + number)" which will result class_1. so there is mismatch in class-1 and class_1

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.