2

I am trying to do the following in jquery

<?php

    echo "<script>";
    echo "$(document).ready(function(){";
    echo "$('#mainbody-wrapper').removeClass('mainbody-wrapper')";
    echo ";";
    echo "$('#mainbody-wrapper').addClass('newStyle')";

    echo ";";
    echo "contentHeight=$('.content').css('height') + 20";
    echo ";";

    echo "alert(contentHeight)";
    echo ";";
    echo "$('#mainbody-wrapper').css('height',contentHeight)";
    echo ";";
    echo "$('#subcontent-wrapper').remove()";
    echo ";";
    echo "})";
    echo "</script>";

?>

But the variable contentHeight output 300px20. I want to do javascript addition, but its getting concatenated. How can this be achieved?

1
  • 2
    Your problem isn't because of PHP; it's a pure JavaScript + jQuery problem. Commented Jul 16, 2011 at 3:54

3 Answers 3

3

If you just want to read the height, jquery has a height method

echo "contentHeight=$('.content').height() + 20";

From the doco:

The difference between .css('height') and .height() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .height() method is recommended when an element's height needs to be used in a mathematical calculation.

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

Comments

1

You can use parseInt():

 echo "contentHeight= parseInt($('.content').css('height')) + 20";

also to go to the next line, use line breaks \n.

example:

echo "contentHeight= parseInt($('.content').css('height')) + 20;\n";

Comments

0

Do you need to echo everything? You know you can run php and javascript together in the same file.

for example:

<?php echo 'hello world'; ?>
<script>
jQuery("<?php echo $var; ?>").addClass("test");
</script>
<?php echo 'end'; ?>

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.