0

My jquery code is appending a piece of HTML in code properly, but the CSS styles are not applied after the HTML has been appended.

What is the right way to do it?

This is how I am doing:

if(listone[i] != "")
{
    var l1pos = listone[i];
    $('ul.ulstep1 li').eq(i).addClass('selected');

    $line1= '<div class="tagless ';
    $line2 = i;
    $line2='><div class="tagleft"></div><span>';
    $line2 += listone[i];
    $line3  = '</span><div class="tagright"></div></div>';
    $('.quiztags').append($line1+$line2+$line3);
    $('.quiztags').append($('<div></div>').attr('class', 'tagless'));
}

This gives me the right HTML but no styling is applied. I am doing this operation inside a javascript onload function, so the CSS should have loaded when this is executed, right?

6
  • 12
    Just eyeballing it and it doesn't look like you're closing your class attribute after $line2 = i; Commented Oct 4, 2011 at 14:02
  • could you post the html that is generated? (via firebug or equivalent), and if the html is valid? Commented Oct 4, 2011 at 14:03
  • The statement '$line2 = i; does nothing as line2 it is overwritten in next line Commented Oct 4, 2011 at 14:12
  • By the way, you could avoid syntax issues like this by using .addClass instead. Something like this: $('<div/>').addClass('tagless').addClass(i).appendTo($('.quiztags')); Commented Oct 4, 2011 at 14:13
  • @Muleskinner good point, still line2 is not closing the class attribute. Commented Oct 4, 2011 at 14:19

1 Answer 1

1

Try it, maybe it works.

if(listone[i] != "")
{
    $('ul.ulstep1 li').eq(i).addClass('selected');
    var line1   = '<div class="tagless ';
    var line2   = i;
    line2       += '"><div class="tagleft"></div><span>';
    line2       += listone[i];
    var line3   = '</span><div class="tagright"></div></div>';
    $('.quiztags').append(line1+line2+line3);
    $('.quiztags').append($('<div></div>').attr('class', 'tagless'));
}
Sign up to request clarification or add additional context in comments.

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.