How is it possible to change the content of this div, when one of the LINKS is clicked?
<div align="center" id="content-container"> <a href="#" class="click cgreen">Main Balance</a> <a href="#" class="click cgreen">PayPal</a> <a href="#" class="click cgreen">AlertPay</a> </div>
Answer: Use the jQuery html() Method You can simply use the jQuery html() method to replace innerHTML of a div or any other element.
For replacing innerHTML of a div with jquery, html() function is used. After loading the web page, on clicking the button content inside the div will be replaced by the content given inside the html() function.
To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
You could subscribe for the .click event for the links and change the contents of the div using the .html
method:
$('.click').click(function() { // get the contents of the link that was clicked var linkText = $(this).text(); // replace the contents of the div with the link text $('#content-container').html(linkText); // cancel the default action of the link by returning false return false; });
Note however that if you replace the contents of this div the click handler that you have assigned will be destroyed. If you intend to inject some new DOM elements inside the div for which you need to attach event handlers, this attachments should be performed inside the .click handler after inserting the new contents. If the original selector of the event is preserved you may also take a look at the .delegate
method to attach the handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With