Change the value of a <H1> with jquery?

Asked

Viewed 761 times

2

I’m making an ajax call and need to change the value of the tag <h1> with the Sponse:

$('#openTickets').ready(function() {
    var open = $.ajax({
        time: 15,
        url: "/opened-tickets",
        dataType: "JSON",
        async: false
    }).responseJSON;

    $('#openTickets').val(open);
});

<div class="value">
    <h1 class="count tkts-total" id="openTickets"></h1>
    <p>Aberto e em dia</p>
</div>
  • 4

    Try using html() instead of val()

  • 1

    Another alternative is to use text() instead of val()

  • @Fleuquerlima It worked, thank you :D

1 answer

4


Some corrections you should take into account.

  • async: false is deprecated, no longer used, was a bad idea of the old that was removed, deprecated.
  • uses $(document).ready( in time of $('#openTickets').ready(, because if you use it as it is jQuery will not find this element if the HTML has not been read, while document is global, made available to Javascript since the beginning of page loading
  • the $.ajax is signing what means you have to use the callback success
  • to change content of HTML elements with jQuery uses .html() or .text(), to change the value of elements that can receive value from the user is used .val(). In the case of a Heading you must use .html() or .text().

So your code can be like this:

$(document).ready(function() {
    $.ajax({
        time: 15,
        url: "/opened-tickets",
        dataType: "JSON",
        success: function(res) {
            $('#openTickets').html(res.responseJSON);
        }
    });
});

Note: I don’t see where you’re going data to ajax, I assume that there is no need.

jsFiddle: http://jsfiddle.net/et90eyzm/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.