1

I need to put in a menu from a script file so that I can only need to make changes in one place to reflect across the project.

$(document).ready(function() 
{
    var str = "<h3>Menu</h3><ul class='nav vertical-nav'>";
    str.append("<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=1' ></i>Chair</a></li>");
    str.append("<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=3' ></i>Secretary</a></li>");
    str.append("<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=2' ></i>Treasurer</a></li>");
    str.append("<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=4' ></i>Admin</a></li>");
    str.append("</ul>");

    $('#insert-menu').html(str);

});

Now the above example is purely appending strings, which does not work. I am new to jquery, so I am learning about the DOM to achieve this.

Now I can find documentation on inserting html files, but not really a lot on how to add code directly.

When I do the <h3> tag by itself it works, but when I start adding attributes (I think) that is where the problem lies.

Am I on the right track here?

4
  • the thing is you cannot get php to dynamically run it is interpreted on runtime Commented Oct 7, 2014 at 19:23
  • But the file in which I am importing this script passes down the variable from the php - so should that not cover it? Commented Oct 7, 2014 at 19:24
  • 1
    no, never used, but php is done with the page rendering it and you pass it dynamically :/ Commented Oct 7, 2014 at 19:30
  • Cool thanks - I will look into it when I improve the code :-) Commented Oct 7, 2014 at 19:37

3 Answers 3

2

To use .append, you need a jQuery object.

var str = "<h3>Menu</h3><ul class='nav vertical-nav'>"; is a string.

Use var str = $("<h3>Menu</h3><ul class='nav vertical-nav'>"); instead.

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

1 Comment

On a side note, you can also use str.appendTo('#insert-menu');.
2

Another way to do this with DOM elements

var menu = $("<ul>");
var item1 = $("<li>").append($("<a>").prop("href","?p=1")).text("Item 1"); 
var item2 = $("<li>").append($("<a>").prop("href","?p=2")).text("Item 2"); //..so on
//..so on
menu.append(item1).append(item2);
$('#insert-menu').append(menu);
//..so on

lets you avoid HTML errors for one.

Comments

0

Use this

$(document).ready(function() 
{
    var str = "<h3>Menu</h3><ul class='nav vertical-nav'>";
    str+="<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=1' ></i>Chair</a></li>";
    str+="<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=3' ></i>Secretary</a></li>";
    str+="<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=2' ></i>Treasurer</a></li>";
    str+="<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=4' ></i>Admin</a></li>";
    str+="</ul>";

    $('#insert-menu').html(str);

});

or

$(document).ready(function() 
{
    var str = $("<h3>Menu</h3><ul class='nav vertical-nav'>");
    str.append("<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=1' ></i>Chair</a></li>");
     str.append("<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=3' ></i>Secretary</a></li>");
     str.append("<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=2' ></i>Treasurer</a></li>");
     str.append("<li><a href='staff_home.php?var01=<?php echo $userVar; ?>&var02=4' ></i>Admin</a></li>");
     str.append("</ul>");

    $('#insert-menu').html(str);

});

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.