Jump to content

JQuery Changing Content


kigogu

Recommended Posts

I'm trying to change content with jquery by changing the content of a div id. Basically what I want to do is when a user clicks on a button, it will change the content and display a new button, then when they click on the new button it will display different content. essentially this will be like a three page website if this were all to work.

 

so far I have:

$(document).ready(function() {

$("#1").click(function() {
$("#leftPart").html('<input type="button" name="submit" id="2" value="Submit" />');
$("#rightPart").html('');
});

$("#2").click(function() {
$("#rightPart").html('<input type="text" id="new_world" value="new_world" name="new_world" >');
});
});

 

In the HTML it will already have a button with an id = 1, so once you click on that it displays the second submit button in the leftPart div, but when you click on the second submit button (id = 2, in the jquery) it doesn't change anything. I'm completely new to jquery, so i could definitely use some help on this.

Link to comment
Share on other sites

NVM! I got the answer, since the second button was created dynamically, it wasn't being registered, so if i just use on instead of click then it works perfectly, here is the new code:

$(document).ready(function() {

$(document).on("click", "#1", function() {
$("#leftPart").html('<input type="button" name="submit" id="2" value="Submit" />');
$("#rightPart").html('');
});

$(document).on("click", "#2", function() {
$("#rightPart").html('<input type="text" id="new_world" value="new_world" name="new_world" >');
});
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.