0

I am currently having an with my jQuery, Below is the code that generates a list via PHP and the list is then clickable to create a list of the sub contents of that folder.

I have a jsfiddle of this working: here

However when run on my page it is not running; the issue seem to be replicated when selecting "No wrap - in ".

I honestly don't know anything about these 4 jsfiddle options, it works in 3 but not 4. This may have been a stupid oversight by me or I may be missing some form of critical info

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<?php
if ($handle = opendir('fileServer')) {
    while (false !== ($file = readdir($handle)))
    {
        if (($file != ".") 
         && ($file != ".."))
        {
            $thelist .= '<LI id='.$file.'>'.$file.'';
        }
    }

    closedir($handle);
}
?>


<script type="text/javascript">
function genCont(){
//  alert(sel + "folder selected")


//This is the alert that works in JSfiddle but not on my Rpi :(
$("#fList li").click(function() {
    alert(this.id); // get id of clicked li used in below PHP
});

//future unused code, suggestion for improvement are, of course, welcome 
//var fName = sel;
<?php
if ($handle = opendir(/*not used yet*/)) {
    while (false !== ($file = readdir($handle)))
    {
        if (($file != ".") 
         && ($file != ".."))
        {
            $thelist2 .= '<LI><a href="'.$file.'">'.$file.'</a>';
        }
        }

    closedir($handle);
}?>
}
</script>


<div class="fsFolder">
<ul id="fList">
<?=$thelist?>   
</ul>
</div>

Thank you ahead of time SO

Kris

3
  • @Sergio: If so, that's a really bad title for the question, given that the issue here is that it does work on jsFiddle, just not in the wild. :-) Commented Sep 2, 2013 at 20:42
  • I realize my title is poor retrospecitively as well as the jsfiddle was slightly wrong, but yes it works on jsfiddle and not on my Rpi. Commented Sep 2, 2013 at 20:46
  • @T.J.Crowder :) I thought it would help OP to understand jsfiddle better. But he got good answers now so I remove my comment. Thanks for the sharp eye :) Commented Sep 2, 2013 at 20:47

2 Answers 2

1

You need to wrap your code with doc.ready

$(document).ready(function(){/*code*/});

UPDATE

$(document).ready(function () {

    $("#fList li").click(function () {
        alert(this.id); // get id of clicked li
    });

    //$boobies = document.getElementByID("#fList li").this.id
    $boobies = $('#fList li')[0].id;

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

3 Comments

Sorry for not fully understanding do you mean the function genContent() or just: $("#fList li").click(function() { alert(this.id); // get id of clicked li used in below PHP }); or is it that whole script tag?
@Ziconius check the update, and btw document.getElementByID doesn't work like jQuery $()
ready handlers are for libraries. There are better options if you control the location of your script elements.
0

You've been hit by jsFiddle's really surprising default behavior, which is to wrap all of your JavaScript code in a window load handler. If you look in the upper left-hand corner of the jsFiddle UI, you'll see an unlabelled drop-down box, with the value onload selected. The window load event happens very late in the page load cycle, after all HTML has been parsed, all external resources (including images) have been loaded, etc.

Just put your script tags at the end of your page, just above </body>, so that the elements they act on exist. The problem with your code is that this line:

$("#fList li").click(...);

happens before that element exists, because it's above it in the page source. Just put it below it in the page source.

References:

1 Comment

So, if I understand correctly the layout should be; decending order <head>CSS etc/<body> content, THEN onclick <script></body> Is this standard or more unique to this situation?

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.