Javascript Logic issue

You could use the same code as in your other thread with minor modifications.