5

Very frustrated here. Can't get this to work and don't know why. I am trying to populate my div with a div from another html file. Here is my code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

<div id="text">
content goes here
</div>

<script type="text/javascript">
    function whenButtonClicked() {
        var book = document.getElementById("book").value; //this is working
        var chapter = document.getElementById("chapter").value; //this is working
        var filename = 'files/' + book + chapter + '.html'; //this is working
        $('#text').load(filename #source); //this is NOT
    }

</script>

Thanks for any help.

Adam

Edit: Here is a link to the actual file I am working on. There may be something wrong in it as well.

6
  • Can you alert the filename and verify that the file exists? It may be a problem with the file path. Commented Apr 4, 2012 at 3:54
  • @mrtsherman I think the op is trying to load only the part of the specified html with id 'source'. Commented Apr 4, 2012 at 3:56
  • @AmitBhargava - thanks, didn't know load could do that! Looks like his syntax is still wrong though. Commented Apr 4, 2012 at 4:02
  • Tried following the info here. I am sure the file exists, it is right beside the file I am using. Commented Apr 4, 2012 at 4:04
  • @preahkumpii Well, I hope the 'files' folder is at the right location. I'm not sure as to the correct absolute location myself. Please post the absolute path of the 'files' folder. Maybe that will help us figure it out. Commented Apr 4, 2012 at 4:10

1 Answer 1

8

Use this code: $('#text').load(filename + " #source");.

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

5 Comments

#source (according to this) is the id of the div in the other html file. I have tried with just 'filename', and will the actually file's name (which is supposed to be dynamic, hence the variable). Nothing works.
Thanks. Jsfiddle doesn't seem to work with nearly any other link I try.
@preahkumpii yes, allright. Google "Access-Control-Allow-Origin".
OK, apparently there were a number of issues, a primary one being that I wasn't doing a hard refresh in the browser. Ok, so now everything plays nice and it will display the info from the other html file nicely. However, it does not work when I use the 'filename' variable. The whole point of this project is to make a look up based on a dynamic selections. So, I think inputting a variable is the problem.
Stupid me. Didn't see it. The problem was not that $.load() cannot take a variable it can. In the line where I define the variable I forgot to put the correct quotes in the string to make it readable to $.load(). So, here is the correct string: var myFile = "'files/" + book + chapter + ".html'";. Thanks for all the help.

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.