1

My question is simple.

I have the following code:

    <div class="last" 
    <?php 
    if hasClass(last){
      echo " style='width:100%;' ";
    } 
    ?> 
    ></div>

I know the if statement is wrong, but the idea is there. I want to know how can I check if this div has the .last class then echo something.

I've been searching around but didn't work anything (didn't find much though).

Best regards.

7
  • You can do it very easy with jQuery if you're ok with that I'll answer your question.
    – Daan
    Feb 11, 2015 at 13:50
  • It can't be done with php that easy, almost sound impossible to me. You can archive this using ajax Feb 11, 2015 at 13:50
  • 2
    @Hudixt ajax isn't necessary
    – Daan
    Feb 11, 2015 at 13:50
  • @Daan I was saying its one way of doing these type of things Feb 11, 2015 at 13:51
  • 2
    It can be done using PHP but you need to parse the DOM to do it - not trivial by any means and almost certainly not worth it for what you're trying to do. It's probably worth re-thinking what you're trying to achieve. If you're set on this you need something like this: symfony.com/doc/current/components/css_selector.html
    – Dan Smith
    Feb 11, 2015 at 13:52

4 Answers 4

1

As already in the comments told it's possible with PHP with DOM parsers.

I'm gonna give you 2 very simple solutions which will save you a lot of work:

CSS:

<style>
.last {
  width:100%;
}
</style>

jQuery:

<script type="text/javascript">
  $(document).ready(function(){
    if($('div').hasClass('last')){
      $('div').css('width', '100%');
    }
  });
</script>
0

PHP runs on server, so it generates HTML. If you have that class="last" you don't need to check - it's part of the code....hard-coded.

But you can have some PHP variable and depending on it's place print out class and also style for that other element:

<?php 
$print_last = true;
?>

...

   <div <?php if ($print_last) echo 'class="last" ';
    <?php 
    if ($print_last){
      echo " style='width:100%;' ";
    } 
    ?> 
    ></div>

But if you want to check on html element you have to do it on client side (browser) from JavaScript and jQuery can be helpful too.

0

It is possible to check that using PHP, however that couldn't be done easliy (you'll need to parse buffered HTML using DOM parser, then look up for divs, etc...).... Much better solution is to do that with Javascript/jQuery, using Document.getElementsByClassName() function.

Sample solution:

var elements = document.getElementsByClassName("last"); 
for(var i = 0; i < elements.length; i++)
{
 var element = elements[i];
 element.style.width = "100%";
}

0

@azhpo Obviously the HTML being the front end language you have to pass the elements either through some submit button or via ajax request.

  1. Using submit button: select the class name of div using either javascript

    var className = document.getElementById("myDIV").className document.getElementById("myHiddenField").value = className;

    Now on clicking the submit button it would get submitted

  2. Using ajax: Again take the classname either through javascript / jquery var className = jQuery("#myDiv").attr("class"); Now fire ajax query and send the class name to your script

    jQuery.ajax({
       url: 'file.php',
       type: 'POST',
       data: 'class='+className,
       success: function(data){//do whatever you want},
       error:function(){//do whatever you want}
    });
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.