0

On a click event, the page loads the game board and several elements are hidden / shown.

Instead of having this giant thing mixed in the function:

$('.playervscomputer').css('display', 'block');
$('.player_vs_cpu_board').css('display', 'block');
$('.player_vs_computer_color_selection').css('display', 'block');
$('.player_vs_computer_start_button').css('display', 'block');
$('.header').css('display', 'block');
$('.hide_answer').css('display', 'block');
$('.intro').remove();
$('.player_board_header').css('display', 'inline');
$('.intro').css('padding-top', 'none');
$('#show_after_starting').css('display', 'inline');
$('#mastermind_table_ten_cpu').css('background-color', 'yellow')

Does anyone know if it's possible to wrap this up in its own function and just call the function when the click event occurs? I tried this, but it was unsuccessful (I'm assuming I did this wrong...)

Thanks

4
  • 1
    Whats stopping you from writing this in a function? Or is it re-use you are trying to achieve? Whats the specific use-case? Commented Apr 26, 2014 at 4:32
  • Please add your html code also. Commented Apr 26, 2014 at 4:33
  • What do you want to do? Hide couple of elements and show some others? Commented Apr 26, 2014 at 4:34
  • Thanks guys. Deepsty's solution was what I was looking for. Appreciate the help everyone. Commented Apr 26, 2014 at 4:47

2 Answers 2

3

Just list them with commas like this:

$('.playervscomputer, .player_vs_cpu_board, .player_vs_computer_color_selection, .player_vs_computer_start_button, .header, .hide_answer').css('display', 'block');
$('.intro').remove();
$('.player_board_header, #show_after_starting').css('display', 'inline');
$('.intro').css('padding-top', 'none');
$('#mastermind_table_ten_cpu').css('background-color', 'yellow')

EDIT: Although in my opinion the best approach will be if u make a css class like this:

.dblock .playervscomputer,.dblock .player_vs_cpu_board, .dblock .player_vs_computer_color_selection, .dblock .player_vs_computer_start_button, .dblock .header, .dblock .hide_answer 
{
 display: block;
}

and just do $("parent-selector").addClass("dblock") to the parent element of all these boxes. This will be the fastest and cleanest approach. Also jQuery queries are expensive. Don't overuse them.

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

2 Comments

@user3007294 I edited my post and added a CSS solution.
Awesome, thanks again. I agree the css class looks a bit cleaner (both look much better than what I had).
0

Well you can chain things like ,And call this function on ready

function init() {
    $('.playervscomputer , .player_vs_cpu_board').css('display', 'block');

} 

$(document).ready(function() {
init();

});

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.