Change content of div - jQuery

Jquery

Jquery Problem Overview


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>

Jquery Solutions


Solution 1 - Jquery

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.

Solution 2 - Jquery

There are 2 jQuery functions that you'll want to use here.

  1. click. This will take an anonymous function as it's sole parameter, and will execute it when the element is clicked.

  2. html. This will take an html string as it's sole parameter, and will replace the contents of your element with the html provided.

So, in your case, you'll want to do the following:

$('#content-container a').click(function(e){
    $(this).parent().html('<a href="#">I\'m a new link</a>');
    e.preventDefault();
});

If you only want to add content to your div, rather than replacing everything in it, you should use append:

$('#content-container a').click(function(e){
    $(this).parent().append('<a href="#">I\'m a new link</a>');
    e.preventDefault();
});

If you want the new added links to also add new content when clicked, you should use event delegation:

$('#content-container').on('click', 'a', function(e){
    $(this).parent().append('<a href="#">I\'m a new link</a>');
    e.preventDefault();
});

Solution 3 - Jquery

$('a').click(function(){
 $('#content-container').html('My content here :-)');
});

Solution 4 - Jquery

You can try the same with replacewith()

$('.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').replaceWith(linkText);

    // cancel the default action of the link by returning false
    return false;
});

The .replaceWith() method removes content from the DOM and inserts new content in its place with a single call.

Solution 5 - Jquery

Try this to Change content of div using jQuery.

See more @ Change content of div using jQuery

$(document).ready(function(){
    $("#Textarea").keyup(function(){
        // Getting the current value of textarea
        var currentText = $(this).val();
        
        // Setting the Div content
        $(".output").text(currentText);
    });
});

Solution 6 - Jquery

Try $('#score_here').html=total;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOliver &#39;Oli&#39; JensenView Question on Stackoverflow
Solution 1 - JqueryDarin DimitrovView Answer on Stackoverflow
Solution 2 - JqueryJoseph SilberView Answer on Stackoverflow
Solution 3 - JqueryJørgenView Answer on Stackoverflow
Solution 4 - JqueryManoj KadolkarView Answer on Stackoverflow
Solution 5 - Jqueryjonathan klevinView Answer on Stackoverflow
Solution 6 - JquerySanjay DevarajanView Answer on Stackoverflow