How to write dynamic text between div tags?

I type div on the login screen. I want this article to be dynamic. This is why I use ajax jQuery to retrieve data. I want this to be written in a div. How do you do this?

<div class ="bas">Write here(title)</div>
$.ajax({
    type: "GET",
    url: MY,
    success: function (msg, result, status, xhr) {
        var obj = jQuery.parseJSON(msg);
        title = obj[0].Write;
    }
});
+4
source share
5 answers

You can use text()or html()to set the contents of the desired item div. Try the following:

<div class="bas">Write here(title)</div>
$.ajax({
    type: "GET",
    url: MY,
    dataType: 'json',
    success: function(msg) {
        $('.bas').text(msg[0].Write);
    }
});

Please note that using dataType: 'json'jQuery will deserialize the answer for you, so you do not need to manually call JSON.parse.

+2
source

html() text() , .
html, <strong> .. use html(), text().

$.ajax({
    type: "GET",
    url:MY,
    success: function (msg, result, status, xhr) {
        var obj = jQuery.parseJSON(msg);
        title= obj[0].Write;
        $('.bas').html(title); // this line
    }
});
0

Use function .text()

<div class ="bas">Write here(title)</div>
$.ajax({
    type: "GET",
    url:MY,
    success: function (msg, result, status, xhr) {
        var obj = jQuery.parseJSON(msg);
        title= obj[0].Write;
        $(".bas").text(title);
    }
});
0
source

Use hanlderbars.js or<div class ="bas">Write here<span class="title"></span></div>

0
source

if only the remote read text file

<div class="bas">Write here (title)</div>

jQuery.get(myURL, /*ifMyParams,*/ function(data){
  jQuery('.bas').text(data);
})
0
source

Source: https://habr.com/ru/post/1666346/


All Articles