0

I have the following in my head tag:

<script type="text/javascript">
    $(function() {
        $.get('content/menu_items.txt', function(data) {
            var menu_items = data.split('\n');
             $.each(menu_items, function(key, val){
                 var menu_items_split = val.split(',');
                    $('#header-bg > .inline_links').append('<a href="'+  menu_items_split[1] +'"><li>'+  menu_items_split[0] +'</li></a>');
             });   
        });   
    });
</script>

I also have a ready function called to add rounded corners to my nav buttons. Everything works fine if I statically add the li tags into the div, but if I use the above code, the corner functions and animations aren't applied. Any idea's why this is?

If you don't understand, here is the full code and comments to help you better understand

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.js"></script>
<script type="text/javascript" src="js/jquery.corner.js"></script>
<script type="text/javascript" src="js/default.js"></script>

<script type="text/javascript">
    $(function() {
        $.get('content/menu_items.txt', function(data) {
            var menu_items = data.split('\n');
             $.each(menu_items, function(key, val){
                 var menu_items_split = val.split(',');
                    $('#header-bg > .inline_links').append('<a href="'+  menu_items_split[1] +'"><li>'+  menu_items_split[0] +'</li></a>');
             });   
        });   
    });
</script>
</head>

<body>

<div id="header-bg">
    <ul class="inline_links">

<li>test</li> //THIS WORKS, ADDS ROUNDED CORNERS AND ANIMATION;

    </ul>
</div>

</body>
</html>
3
  • Do you call the function that you use to add the rounded corners after running the $.GET ? Commented Apr 26, 2012 at 21:15
  • no, i have a ready function inside default.js that should apply to all <li> items inside the inline_links class. I tried moving the script before default.js is loaded but same results. Commented Apr 27, 2012 at 9:10
  • problem solved. switched .get function to .ajax with async off and works just fine. Commented Apr 27, 2012 at 12:49

2 Answers 2

1

Depending on your CSS, I would suspect that you should put the <li> tag outside the <a>;

$('#header-bg > .inline_links').append('<li><a href="'+  menu_items_split[1] +'">'+  menu_items_split[0] +'</a></li>');
Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't matter how his CSS looks like - a <a> inside <ul> but outside <li> makes no sense.
Note also that this also makes your code valid
@ThiefMaster - I agree with you (see my further comment) but this was not the question.
@MattTew and ThiefMaster, +'d for the syntax awareness :)
thanks for the tip, I have moved the <a> tags inside the <li>.
1

In order for the Jquery Corner to be applied to you newly created li, you can try this:

JQUERY:

$(function() {
  $.get('content/menu_items.txt', function(data) {
    var menu_items = data.split('\n');
    $.each(menu_items, function(key, val){
      var menu_items_split = val.split(',');
      $('#header-bg > .inline_links').append('<li><a href="'+menu_items_split[1]+'">'+  menu_items_split[0] +'</a></li>');
      // the new line to try
      $('#header-bg > .inline_links li').last().corner();
    });   
  });   
});

The new line will target the last <li>, the one just appended and apply the corner script to it.

4 Comments

+'d because thinking about it, your's is probably the correct answer.
this indeed works thank you. However is there anyway to automatically apply it from within another .js file? I have code inside a default.js file that should automatically apply it along with some animation code.
problem solved. switched .get function to .ajax with async off and works just fine.
Well, thats one way, but keep in mind that with async off, the page will stay waiting for does contents to be retrieved, thus increasing the overall load time.

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.