0

Performing a basic AJAX request, but when the HTML is displayed no HTML links work and now CSS:hover elements work either.

Confused as to what I've done wrong. Here's an example of the code below.

CSS:

li:hover{ background:red; }
a{ text-decoration:none; }
a:hover{ text-decoration:underline; }

HTML (index.php):

<script>
$(document).ready(function(){
    ajax();
});
</script>
<ul class="loadhere"></uL>

loadthis.php:

<li><a href="">Example</a></li>

JS (AJAX):

function ajax(){
$.ajax({
    type:"POST",
    url:"http://example.com/loadthis.php",
    dataType: "html" 
})
.done(function(result){
    $('ul.loadhere').html(result);      
});
}
11
  • How looks like 'result'? Commented May 23, 2013 at 10:18
  • possibly the paths are relative to the example.com/loadthis.php - so it can't find the css file. You can change the paths to absolute, so for the CSS for example: example.com/css/style.css Commented May 23, 2013 at 10:19
  • If you take ajax out of the question for now and just add an <li> to the list does the CSS work for that? Commented May 23, 2013 at 10:22
  • 2
    The provided code works - jsfiddle.net/3WBbn Commented May 23, 2013 at 10:29
  • 1
    As @Andreas said, this should work... unless you are cross-domain! Commented May 23, 2013 at 10:33

2 Answers 2

1

You need to use the "success" option of ajax function, take off the done function and insert the function in success option in ajax object, like this:

function ajax(){
    $.ajax({
        type:"POST",
        url:"http://example.com/loadthis.php",
        dataType: "html" ,
        success: function(result){
            $('ul.loadhere').html(result);      
        }
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Still no luck unfortunately.
0

Try this

 <script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<script>
        $(function(){
            ajax();
            function ajax(){
                $.get('loadthis.php', {}, function(){

                }).done(function(result){
                    $('ul.loadhere').html(result);
                });
            }
        });
    </script>

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.