0

In My HTML Page I want to display certain Number of Buttons Based on the Count. So i used to display these buttons Dynamically. Fot That I Have written

var html = "<a href='#' data-role='button' data-theme='b' data-inline='true'>Pay</a>";
$("#DivId").append(html);

But The Styling is not applied to the Button. When I Am Trying to Write the below code in Div Tag Directly It works fine

<a href='#' data-role='button' data-theme='b' data-inline='true'>Pay</a>

Any Help ?

5
  • Possible dupe: stackoverflow.com/questions/5562461/… Commented Jun 25, 2012 at 20:14
  • What does data-inline, data-theme do? Why not use a css class, class="myclass", and define in <style> tag? Commented Jun 25, 2012 at 20:14
  • 1
    can you post the CSS you're using to style the anchor? Commented Jun 25, 2012 at 20:15
  • @nobugs He didn't say so, but this looks like JQueryMobile code. JQM will automatically add styles and such based on the different theme/role/options that are set. Commented Jun 25, 2012 at 20:16
  • @AndrewR Cool, looks like a multi-theme setup similar to adobe.com/devnet/dreamweaver/articles/… Commented Jun 25, 2012 at 20:21

3 Answers 3

4

You have to allow JQueryMobile to update for the newly added DOM elements.

See http://jquerymobiledictionary.pl/faq.html

var html = "<a href='#' data-role='button' data-theme='b' data-inline='true'>Pay</a>";
$("#DivId").append(html).trigger('create');
Sign up to request clarification or add additional context in comments.

Comments

1

Have you used the styling by JavaScript on $(document).ready() function or something? In that case, you need to reinitate the function after the execution of $("#DivId").append(html);.

Example

You have a function like this:

$(document).ready(function(){
    $('a[href="#modal"]').click(function(){
        alert("Modal");
    });
});

It gets executed in the runtime, after the page loads. You are dynamically inserting another <a> tag with the same stuff. Eg:

$("#result").html('<a href="#modal">Modal Window</a>');

What happens is, this HTML is inserted after the handler is executed. So, the handler initiated in the $(document).ready() function is not applicable for this. So, in order to make sure that even this gets executed, you need to reinitialize it this way:

$(document).ready(function(){
    loadPage();
});
function loadPage(){
    $('a[href="#modal"]').click(function(){
        alert("Modal");
    });
}
function something(){
    $("#result").html('<a href="#modal">Modal Window</a>');
    loadPage();
}

Hope you got it?

3 Comments

Can You Give me an example for this ?
I Have Used External CSS Styling
I gave you the detailed explanation @user1198485. Check it out, and see if you are able to understand it. :)
0

The answer is it should work. As long as the code outputs properly in the DOM (see with Firebug or other inspector) there is no reason CSS won't work on it, and most likely the problem is with your CSS that you haven't shared.

Comments

Your Answer

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