I would like to replace a string with a random string from an array if that string equals a certain condition.
So far I have this (which doesn't address the condition part).
the html:
<div>
<span class ="test">foo</span>
</div>
<div>
<span class ="test">bar</span>
</div>
<div>
<span class ="test">test</span>
</div>
<div>
<span class ="test">random</span>
</div>
the code:
$(".test").each(function () {
var quotes = new Array("foo", "bar", "baz", "chuck"),
randno = quotes[Math.floor(Math.random() * quotes.length)];
$('.test').text(randno);
});
This sets every ".test" class the same thing. I get:
foo
foo
foo
foo
or
bar
bar
bar
bar
How do I make this only replace the string if it equals say "foo"?
If I have multiple "foos" How do i get each "foo" it replaces to be random not all set to the same thing?