Leahcim
Leahcim

Reputation: 41919

jQuery- Using a variable as a selector?

In this fiddle, http://jsfiddle.net/5L8Q8/52/ if you click the black box, it randomly selects either "ONE" or "TWO" from an array and assigns that value to "ran."

There are also two divs on the page "ONE" or "TWO" with text content in each. These divs are unrelated to the values selected from the array

Depending on the value assigned to ran, I want to express the text contents of div "ONE" or "TWO" inside div "result"...

I was playing around with something like this below but made no progress

$('#result').html(function() {
  //some function to put contents of either div ONE or TWO (depending on ran) inside of #result
});

Upvotes: 0

Views: 89

Answers (3)

citizen conn
citizen conn

Reputation: 15390

Here it is:

$('#button').click(function() { 
  ran = getRandom(myArray, true); 
  $('#result').html($('#'+ran).html());
});

http://jsfiddle.net/5L8Q8/62/

Upvotes: 0

Paul
Paul

Reputation: 141839

Like this: http://jsfiddle.net/Paulpro/5L8Q8/54/

$('#result').html($('#'+ran).html());

Upvotes: 0

James Montagne
James Montagne

Reputation: 78630

http://jsfiddle.net/5L8Q8/57/

$("#button").click(function() {
    ran = getRandom(myArray, true);

    $('#result').html($("#" + ran).html()); 
});

Upvotes: 5

Related Questions