-1

I am trying to append a new list item based on a condition using jquery. For reference the page is a Wordpress page and i have been able to implement serval jquery scripts using same format.

Here is the sample HTML.

<div class = "bag" id = "bag" style="width: 100%; padding: 0 0 0 5px; float: left;">
<ul id = "clubsli" name = "clubsli" class = "clubsli">

I dynamically create a li with "n" values and want to append a new li to the end of this list using JavaScript/jQuery script.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        function addClub() {
            $roleint = 2613;
            $count = $("#clubsli li").length - 1;
            if ($roleint == 2613) {
                if ($count < 100) {
                    $("#bag ul").append('<li>$roleinit</li>');
                };
            };
        };
    });
</script>

No matter what I do I get the following Unexpected token ILLEGAL error that seems to not like the "<" in the list element.

Here is the error I receive.

console error

I've tried appending a non-li and can without fail and tried "appendTo" but get same error on the "<".

oldnewbe
  • 23
  • 1
  • 7
  • As a note, i have copied the script into Notepad++ and verified there are no hidden characters. arggg. – oldnewbe Apr 03 '16 at 17:25
  • You shouldn't have spaces between the attributes and their value, so `id = "clubsli"` should be `id="clubsli"` for example. – doublesharp Apr 03 '16 at 17:27
  • I also suspect the error is caused by not closing a quote somewhere not included in your example. – doublesharp Apr 03 '16 at 17:29
  • Thanks for the tips @doublesharp. i will try them. The JS errors on the above, unless you think the missing quotes are in the HTML list. however the list renders fine. – oldnewbe Apr 03 '16 at 17:55
  • 2
    @doublesharp The spaces are fine. http://stackoverflow.com/questions/7064095/spaces-between-html-attributes-and-values – Matt Gibson Apr 03 '16 at 18:09
  • 5
    You can't have line breaks inside strings. The code in the question doesn't match the code in the screenshot. – JJJ Apr 03 '16 at 18:10
  • I'd agree that it looks like there's something going on that hasn't been posted. Can you make a small, *complete* example that definitely shows the problem? (And where did that `

    ` element in the console come from, that doesn't appear anywhere in the posted source?)

    – Matt Gibson Apr 03 '16 at 18:15
  • sorry, i am not sure what you mean. the JS code posted is complete and the picture of the chrome console is for this code. For some reason either the JS parser or Chrome is inserting something like the `

    ` but i don't know why or how to prevent it.

    – oldnewbe Apr 03 '16 at 18:21
  • Wait, is this a Wordpress post? It's Wordpress's editor that's inserting the line breaks. – JJJ Apr 03 '16 at 18:32
  • @oldnewbe: It's probably not the JS parser or Chrome. It's probably your web server. – recursive Apr 03 '16 at 18:35
  • Yes, i included Wordpress as one of the tags an editor removed it for some reason. I don't think it's the Wordpress editor since I can copy the JS into Notepad++ and the "inserted" characters are not there. Only when the Wordpress parser, or jquery parser or chrome handles it do the characters get added. – oldnewbe Apr 03 '16 at 18:35
  • 1
    What's the difference between "Wordpress parser" and the Wordpress editor? Your logic doesn't make much sense. If the characters are not in the original code, then Wordpress must insert them. It's 100% certain that Chrome doesn't do it. There is no "jQuery parser". – JJJ Apr 03 '16 at 18:37
  • The tag was probably removed because there's nothing much in your question about WordPress. However, it's almost certainly the way you're getting your code out through WordPress that's actually causing your problem. Please describe *exactly* how you are using WordPress to output your code; that might help us figure out what's happening to it on the way through. (Once you've done that, it's likely you can put the WordPress tag back on and have it stay on.) – Matt Gibson Apr 03 '16 at 18:39
  • Not sure why this was downvoted. If was because I am unsure of developer lingo, I can accept that because this is my first programming attempt. If it was because you believe I did not attempt to investigate the answer myself, the original text of my post says that I tried several options to no avail. The very first line in my post says it is a WordPress page and that is why it had the WP tag. Not sure where to go from here. – oldnewbe Apr 04 '16 at 00:16

2 Answers2

1

Your code is basically okay. Here's a working JSFiddle version of it, with the following problems or incompletenesses corrected:

$(document).ready(function () {
    function addClub() {
        $roleint = 2613;
        $count = $("#clubsli li").length - 1;
        if ($roleint == 2613) {
            if ($count < 100) {
                $("#bag ul").append('<li>' + $roleint + '</li>');
            };
        };
    };
    addClub();
});
  • You needed to use the string appending operator + in your appended string.
  • You'd typo'd $roleint as $roleinit in your append()
  • You'd declared the addClub() function, but not actually called it. I presume it was being called somewhere else in your code?

In addition, as Guido points out, it's quite odd to use variable names starting with $ in jQuery unless the variable is actually a jQuery object, so that makes your code read quite strangely.

I don't think any of this would have caused the error in your question, though, so perhaps there's something else going on that we can't see from your description?

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • Matt, thanks for the help. I am beginning to think this is a problem with Wordpress as my page is a Wordpress page that has a combination of PHP and Javascrpt/Jquery in it. – oldnewbe Apr 03 '16 at 18:50
  • Matt, thanks for the help. I am beginning to think this is a problem with Wordpress as my page is a Wordpress page that has a combination of PHP and Javascrpt/Jquery in it. I have changed the `append`ed string as you suggest. No matter what i do when i save the page and view it in my Chrome browser, the same error occurs. Either the Wordpress parser or Jquery parser does not like the `<` in the `.append('
  • ` and throws the token illegal error and seems to add in `

    ` at the end of the appended list item...

  • – oldnewbe Apr 03 '16 at 18:56