0

I am new to JQuery Mobile and trying to build a simple app. Part of the codes as follows:

  <script type="text/javascript">
    $(document).ready(function(){
         $(".trycss").css({
            'background': 'red' });
    $.post("http://getcheckout.php", 
        function(data){
            var CheckOutListHTML ='<p class="trycss"> HELLO</p>';
                $('.class-ItemsList').append(CheckOutListHTML);
            },"json");
    });

</script>

<div data-role="page" id="id-ItemsPage">
    <div data-role="header" data-position="fixed">
        <h1>Items</h1>
    </div><!-- /header -->
    <div data-role="content">   
        <div class="class-ItemsList">
        </div>
    </div><!-- /content -->
</div><!-- /page -->

So, I am trying to use the "trycss" to style something I am creating dynamically. But it is not working. Can someone help?

Thanks.

2 Answers 2

1

Try following:

 <style>
   .trycss { background-color: red; }
 </style>
 <script type="text/javascript">
    $(document).ready(function(){
    $.post("http://getcheckout.php", 
        function(data){
            var CheckOutListHTML ='<p class="trycss"> HELLO</p>';
                $('.class-ItemsList').append(CheckOutListHTML);
            },"json");
    });

</script>

<div data-role="page" id="id-ItemsPage">
    <div data-role="header" data-position="fixed">
        <h1>Items</h1>
    </div><!-- /header -->
    <div data-role="content">   
        <div class="class-ItemsList">
        </div>
    </div><!-- /content -->
</div><!-- /page -->
Sign up to request clarification or add additional context in comments.

Comments

0

$(".trycss").css(... is executed once and applies styles (not "a style sheet") to all matching elements that exist at that point of time ans you don't you create your element until later on.

What exactly are you attempting to do? Why are you using $(".trycss").css(... and not a style sheet like @hardik suggests?

1 Comment

Yes, I should be using style sheet I guess. I am new to these stuff and is just trying different things out for my app.

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.