0

I want to replace a div container with jQuery onclick on a button, but I don't know how to do this. I tried this:

jQuery(document).click(function() {
("#dynamic").load("single.php #partial_index");
});

But how do I replace it? Now it won't show up if I click on it (it does nothing), but that's normal I guess because I don't state replacing the div. Is there any change I can get this work?

I want to replace a div with an ID called test if I click on the button dynamic. I want it to be replaced by single.php #partial_index. The button does nothing now.

4 Answers 4

3

First, always make sure your primary code is INSIDE the DOM ready structure, like this:

jQuery(document).ready(function() {
    // This fires when the DOM has loaded (thus all your link(stylesheet) and script includes at the very least
});

Inside your function, I see you using ('#dynamic'), wheras it is missing the $ jQuery global or the jQuery namespace itself, use it like so:

jQuery('#dynamic');

Or

$('#dynamic');

Next, you've got a button you'd want to use to replace content inside a div?

$('button').click(function() {
    $('#dynamic').load('single.php#partial_index');
});

You can also REPLACE your content with a new element, like so:

$('#dynamic').replaceWith($('<div>I am a new jQuery dynamic element!</div>'));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, I tried this: <script> jQuery(document).ready(function() { jQuery('button').click(function() { jQuery('#dynamic').replaceWith(jQuery('<div>I am a new jQuery dynamic element!</div>')); })}); </script> But it does nothing?
1

What this code does now is bind a click handler to the document. When the document is clicked, it loads single.php #partial_index into the #dynamic element (though it's missing the $: $('#dynamic').

Here's what I think you want:

$('#dynamic').click(function() {
    $('#test').load("single.php #partial_index");
});

3 Comments

It is no answer to my question. It doesn't replace a thing.
Here's how I understand what you want: "When I click the element with the id of 'dynamic', fetch 'single.php' (only the contents of the element with id 'partial_index') and put that content in the div with the id of 'test'". Am I understanding incorrectly?
I want to replace test with partial_index, but the way you understood would work too. Anyhow, this was just a small part of a different question. Here is the actual question: stackoverflow.com/questions/17751511/…
1

Untested but something like this should work

$("#dynamic").click(function() {
    $("<div />").attr("id", "#partial_index").load("single.php #partial_index", function(data){
         var newDiv = $(this).html(data);
         $('#test').replaceWith(newDiv)  
     })
});

1 Comment

Thanks for the answer James, but nothing is happening either.
0
$("#dynamic").click(function() {
   $('#test').replaceWith($('#partial_index'));
});

2 Comments

Nope, it says missing ) after argument list </script>
I have edited my answer to close the braces.. see if you have closed the braces properly

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.