Kyle Yeo
Kyle Yeo

Reputation: 2368

Changing CSS styles with PHP and jQuery

I'm executing a PHP if/else statement.

However, I want the code to do something like this :

<?php 

    if ($condition == true){ 
       echo 'ITS TRUE!'; 
    }else { 
       #id-element.css('display':'none'); 
    } 

?>

Note that the ELSE statement executes a jQuery line that changes the css style of the element involved.

How do i go about doing this?

Anyone have any suggestions / examples of how I could implement a PHP if/else and change css styles with jQuery inside the PHP if/else statement?

Upvotes: 11

Views: 52006

Answers (2)

clean_coding
clean_coding

Reputation: 1166

Reckon you could do something like this:

<?php
    if ($condition == true): 
         echo 'ITS TRUE!'; 
    else: 
?> 
    //put whatever html/style/script you want here, for example
    <script>
        $( '#id-element' ).css('display', 'none');
    </script>
<?php endif; ?>

Upvotes: 6

Will
Will

Reputation: 20235

You can echo the HTML for a style element and throw the CSS inside that.

else {
    echo '<style type="text/css">
        #id-element {
            display: none;
        }
        </style>';
}

Upvotes: 33

Related Questions