jquery ajax request wrapped in jQuery()'s .text() isn't the same as the div's .text()?











up vote
2
down vote

favorite












I know this is a very concrete issue but I've struggled with this code for the last 2 hours and I can't find anything on the topic or figure out how to solve it.



I have an issue with a jQuery Ajax Post request. I am trying to make a section on my page where I display the users current level. When a user interacts and does something that increases the level on the site the level should also increase in the DOM/client's browser. Therefore I've added a setinterval that allows the request to run every 5th second. And if the $(response).text() is different from the current div's .text() where i am rendering the current level I want to append this new response from the ajax request.



Maybe the answer is more obvious than I think.... I have added the code below



Ajax page:



$(document).ready(function () {

function getLevel() {

var getLvlData = $.ajax({
url: "AjaxRequests.php",
type: "POST",
data: {type: "4", lvl_r_type: 1}
});

getLvlData.done(function (response, textStatus, jqXHR) {
var innerLevelHTML = $("#LevelContainer");

if (($(response).text()) != ($(innerLevelHTML).text())) {
innerLevelHTML.html("");
innerLevelHTML.append(response);

}
if ($(response).text() == $(innerLevelHTML).text()) {
alert("the same");
}
});

}

var levelChecker = setInterval(function () {
getLevel();
}, 1000);
getLevel();

});


AjaxRequests.php:



    if ($_POST["type"] == "4" && isset($_POST["lvl_r_type"])) {

$returnVal = htmlentities($_POST["lvl_r_type"]);

if (!empty($returnVal)) {
if ($returnVal == 1) {

?>

<div id="chartWrapper">
<div>
<canvas id="doughnut-chart"></canvas>
</div>
<div id="chart-center">
<div>
<div><h1>0%</h1></div>
<div><p>GX 0 / 1</p></div>
</div>
<div><p>LVL 0</p></div>
</div>
</div>

<div id="progressList">
<div><p>View tasks</p></div>
</div>

<?php

}
}
}
?>


html page



                    <div id="LevelContainer"></div>









share|improve this question




















  • 1




    Why not upload images of code on SO when asking a question?
    – charlietfl
    Nov 10 at 23:42






  • 1




    Post code, not pictures of code, thanks.
    – Robert Rocha
    Nov 10 at 23:43










  • Sry, guys. Give me 2 min
    – gamernes konge
    Nov 10 at 23:44










  • I have added the code now!
    – gamernes konge
    Nov 11 at 0:25










  • Could I ask you to please update your title because request.text() is a valid method for the fetch API, and not valid for the jqXHR object it's jQuery().text on jqXHR response does not match text already in HTML when it should
    – Martin Barker
    Nov 11 at 1:23

















up vote
2
down vote

favorite












I know this is a very concrete issue but I've struggled with this code for the last 2 hours and I can't find anything on the topic or figure out how to solve it.



I have an issue with a jQuery Ajax Post request. I am trying to make a section on my page where I display the users current level. When a user interacts and does something that increases the level on the site the level should also increase in the DOM/client's browser. Therefore I've added a setinterval that allows the request to run every 5th second. And if the $(response).text() is different from the current div's .text() where i am rendering the current level I want to append this new response from the ajax request.



Maybe the answer is more obvious than I think.... I have added the code below



Ajax page:



$(document).ready(function () {

function getLevel() {

var getLvlData = $.ajax({
url: "AjaxRequests.php",
type: "POST",
data: {type: "4", lvl_r_type: 1}
});

getLvlData.done(function (response, textStatus, jqXHR) {
var innerLevelHTML = $("#LevelContainer");

if (($(response).text()) != ($(innerLevelHTML).text())) {
innerLevelHTML.html("");
innerLevelHTML.append(response);

}
if ($(response).text() == $(innerLevelHTML).text()) {
alert("the same");
}
});

}

var levelChecker = setInterval(function () {
getLevel();
}, 1000);
getLevel();

});


AjaxRequests.php:



    if ($_POST["type"] == "4" && isset($_POST["lvl_r_type"])) {

$returnVal = htmlentities($_POST["lvl_r_type"]);

if (!empty($returnVal)) {
if ($returnVal == 1) {

?>

<div id="chartWrapper">
<div>
<canvas id="doughnut-chart"></canvas>
</div>
<div id="chart-center">
<div>
<div><h1>0%</h1></div>
<div><p>GX 0 / 1</p></div>
</div>
<div><p>LVL 0</p></div>
</div>
</div>

<div id="progressList">
<div><p>View tasks</p></div>
</div>

<?php

}
}
}
?>


html page



                    <div id="LevelContainer"></div>









share|improve this question




















  • 1




    Why not upload images of code on SO when asking a question?
    – charlietfl
    Nov 10 at 23:42






  • 1




    Post code, not pictures of code, thanks.
    – Robert Rocha
    Nov 10 at 23:43










  • Sry, guys. Give me 2 min
    – gamernes konge
    Nov 10 at 23:44










  • I have added the code now!
    – gamernes konge
    Nov 11 at 0:25










  • Could I ask you to please update your title because request.text() is a valid method for the fetch API, and not valid for the jqXHR object it's jQuery().text on jqXHR response does not match text already in HTML when it should
    – Martin Barker
    Nov 11 at 1:23















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I know this is a very concrete issue but I've struggled with this code for the last 2 hours and I can't find anything on the topic or figure out how to solve it.



I have an issue with a jQuery Ajax Post request. I am trying to make a section on my page where I display the users current level. When a user interacts and does something that increases the level on the site the level should also increase in the DOM/client's browser. Therefore I've added a setinterval that allows the request to run every 5th second. And if the $(response).text() is different from the current div's .text() where i am rendering the current level I want to append this new response from the ajax request.



Maybe the answer is more obvious than I think.... I have added the code below



Ajax page:



$(document).ready(function () {

function getLevel() {

var getLvlData = $.ajax({
url: "AjaxRequests.php",
type: "POST",
data: {type: "4", lvl_r_type: 1}
});

getLvlData.done(function (response, textStatus, jqXHR) {
var innerLevelHTML = $("#LevelContainer");

if (($(response).text()) != ($(innerLevelHTML).text())) {
innerLevelHTML.html("");
innerLevelHTML.append(response);

}
if ($(response).text() == $(innerLevelHTML).text()) {
alert("the same");
}
});

}

var levelChecker = setInterval(function () {
getLevel();
}, 1000);
getLevel();

});


AjaxRequests.php:



    if ($_POST["type"] == "4" && isset($_POST["lvl_r_type"])) {

$returnVal = htmlentities($_POST["lvl_r_type"]);

if (!empty($returnVal)) {
if ($returnVal == 1) {

?>

<div id="chartWrapper">
<div>
<canvas id="doughnut-chart"></canvas>
</div>
<div id="chart-center">
<div>
<div><h1>0%</h1></div>
<div><p>GX 0 / 1</p></div>
</div>
<div><p>LVL 0</p></div>
</div>
</div>

<div id="progressList">
<div><p>View tasks</p></div>
</div>

<?php

}
}
}
?>


html page



                    <div id="LevelContainer"></div>









share|improve this question















I know this is a very concrete issue but I've struggled with this code for the last 2 hours and I can't find anything on the topic or figure out how to solve it.



I have an issue with a jQuery Ajax Post request. I am trying to make a section on my page where I display the users current level. When a user interacts and does something that increases the level on the site the level should also increase in the DOM/client's browser. Therefore I've added a setinterval that allows the request to run every 5th second. And if the $(response).text() is different from the current div's .text() where i am rendering the current level I want to append this new response from the ajax request.



Maybe the answer is more obvious than I think.... I have added the code below



Ajax page:



$(document).ready(function () {

function getLevel() {

var getLvlData = $.ajax({
url: "AjaxRequests.php",
type: "POST",
data: {type: "4", lvl_r_type: 1}
});

getLvlData.done(function (response, textStatus, jqXHR) {
var innerLevelHTML = $("#LevelContainer");

if (($(response).text()) != ($(innerLevelHTML).text())) {
innerLevelHTML.html("");
innerLevelHTML.append(response);

}
if ($(response).text() == $(innerLevelHTML).text()) {
alert("the same");
}
});

}

var levelChecker = setInterval(function () {
getLevel();
}, 1000);
getLevel();

});


AjaxRequests.php:



    if ($_POST["type"] == "4" && isset($_POST["lvl_r_type"])) {

$returnVal = htmlentities($_POST["lvl_r_type"]);

if (!empty($returnVal)) {
if ($returnVal == 1) {

?>

<div id="chartWrapper">
<div>
<canvas id="doughnut-chart"></canvas>
</div>
<div id="chart-center">
<div>
<div><h1>0%</h1></div>
<div><p>GX 0 / 1</p></div>
</div>
<div><p>LVL 0</p></div>
</div>
</div>

<div id="progressList">
<div><p>View tasks</p></div>
</div>

<?php

}
}
}
?>


html page



                    <div id="LevelContainer"></div>






javascript php jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 16:17









Martin Barker

4,2022458




4,2022458










asked Nov 10 at 23:40









gamernes konge

163




163








  • 1




    Why not upload images of code on SO when asking a question?
    – charlietfl
    Nov 10 at 23:42






  • 1




    Post code, not pictures of code, thanks.
    – Robert Rocha
    Nov 10 at 23:43










  • Sry, guys. Give me 2 min
    – gamernes konge
    Nov 10 at 23:44










  • I have added the code now!
    – gamernes konge
    Nov 11 at 0:25










  • Could I ask you to please update your title because request.text() is a valid method for the fetch API, and not valid for the jqXHR object it's jQuery().text on jqXHR response does not match text already in HTML when it should
    – Martin Barker
    Nov 11 at 1:23
















  • 1




    Why not upload images of code on SO when asking a question?
    – charlietfl
    Nov 10 at 23:42






  • 1




    Post code, not pictures of code, thanks.
    – Robert Rocha
    Nov 10 at 23:43










  • Sry, guys. Give me 2 min
    – gamernes konge
    Nov 10 at 23:44










  • I have added the code now!
    – gamernes konge
    Nov 11 at 0:25










  • Could I ask you to please update your title because request.text() is a valid method for the fetch API, and not valid for the jqXHR object it's jQuery().text on jqXHR response does not match text already in HTML when it should
    – Martin Barker
    Nov 11 at 1:23










1




1




Why not upload images of code on SO when asking a question?
– charlietfl
Nov 10 at 23:42




Why not upload images of code on SO when asking a question?
– charlietfl
Nov 10 at 23:42




1




1




Post code, not pictures of code, thanks.
– Robert Rocha
Nov 10 at 23:43




Post code, not pictures of code, thanks.
– Robert Rocha
Nov 10 at 23:43












Sry, guys. Give me 2 min
– gamernes konge
Nov 10 at 23:44




Sry, guys. Give me 2 min
– gamernes konge
Nov 10 at 23:44












I have added the code now!
– gamernes konge
Nov 11 at 0:25




I have added the code now!
– gamernes konge
Nov 11 at 0:25












Could I ask you to please update your title because request.text() is a valid method for the fetch API, and not valid for the jqXHR object it's jQuery().text on jqXHR response does not match text already in HTML when it should
– Martin Barker
Nov 11 at 1:23






Could I ask you to please update your title because request.text() is a valid method for the fetch API, and not valid for the jqXHR object it's jQuery().text on jqXHR response does not match text already in HTML when it should
– Martin Barker
Nov 11 at 1:23














1 Answer
1






active

oldest

votes

















up vote
1
down vote













Try using trim since a white space could be treated as a part of the text.



if (($(response).text().trim()) != ($(innerLevelHTML).text().trim())) {
...


One alternative approach I can think of, off of my head is, to set a hash inside a data attribute say data-hash="" whenever you set the html of that div, and then whenever you have a new ajax response you can generate a hash on the fly and compare that to the existing one and if a change is found just update the html and the hash value for that attribute.



This process minimises the confusion that may be caused due to dom being updated by some script.



Also, Instead of



innerLevelHTML.html("");
innerLevelHTML.append(response);


Why not just just set the html directly as



innerLevelHTML.html(response);





share|improve this answer



















  • 1




    Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery function jQuery(...) or $(...) it will create a DOM Element for you, to test this do $("<div></div>"); while I do agree with your answers main point about using .html() it's cleaner and more efficient to just overnight the content once you have the text in memory already. so please remove your first line as it's not true.
    – Martin Barker
    Nov 11 at 1:03












  • Ahh it does work...My bad, Updated my answer. I don't know how I missed that. :)
    – Mohd Abdul Mujib
    Nov 11 at 1:12










  • I think I now know why you missed it, because the title is wrong and suggest that the OP is using .text on a jqXHR when they are not.
    – Martin Barker
    Nov 11 at 1:26












  • He he, on point, my thought process was pretty much something along those lines... thanks.
    – Mohd Abdul Mujib
    Nov 11 at 1:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244510%2fjquery-ajax-request-wrapped-in-jquerys-text-isnt-the-same-as-the-divs-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Try using trim since a white space could be treated as a part of the text.



if (($(response).text().trim()) != ($(innerLevelHTML).text().trim())) {
...


One alternative approach I can think of, off of my head is, to set a hash inside a data attribute say data-hash="" whenever you set the html of that div, and then whenever you have a new ajax response you can generate a hash on the fly and compare that to the existing one and if a change is found just update the html and the hash value for that attribute.



This process minimises the confusion that may be caused due to dom being updated by some script.



Also, Instead of



innerLevelHTML.html("");
innerLevelHTML.append(response);


Why not just just set the html directly as



innerLevelHTML.html(response);





share|improve this answer



















  • 1




    Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery function jQuery(...) or $(...) it will create a DOM Element for you, to test this do $("<div></div>"); while I do agree with your answers main point about using .html() it's cleaner and more efficient to just overnight the content once you have the text in memory already. so please remove your first line as it's not true.
    – Martin Barker
    Nov 11 at 1:03












  • Ahh it does work...My bad, Updated my answer. I don't know how I missed that. :)
    – Mohd Abdul Mujib
    Nov 11 at 1:12










  • I think I now know why you missed it, because the title is wrong and suggest that the OP is using .text on a jqXHR when they are not.
    – Martin Barker
    Nov 11 at 1:26












  • He he, on point, my thought process was pretty much something along those lines... thanks.
    – Mohd Abdul Mujib
    Nov 11 at 1:29















up vote
1
down vote













Try using trim since a white space could be treated as a part of the text.



if (($(response).text().trim()) != ($(innerLevelHTML).text().trim())) {
...


One alternative approach I can think of, off of my head is, to set a hash inside a data attribute say data-hash="" whenever you set the html of that div, and then whenever you have a new ajax response you can generate a hash on the fly and compare that to the existing one and if a change is found just update the html and the hash value for that attribute.



This process minimises the confusion that may be caused due to dom being updated by some script.



Also, Instead of



innerLevelHTML.html("");
innerLevelHTML.append(response);


Why not just just set the html directly as



innerLevelHTML.html(response);





share|improve this answer



















  • 1




    Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery function jQuery(...) or $(...) it will create a DOM Element for you, to test this do $("<div></div>"); while I do agree with your answers main point about using .html() it's cleaner and more efficient to just overnight the content once you have the text in memory already. so please remove your first line as it's not true.
    – Martin Barker
    Nov 11 at 1:03












  • Ahh it does work...My bad, Updated my answer. I don't know how I missed that. :)
    – Mohd Abdul Mujib
    Nov 11 at 1:12










  • I think I now know why you missed it, because the title is wrong and suggest that the OP is using .text on a jqXHR when they are not.
    – Martin Barker
    Nov 11 at 1:26












  • He he, on point, my thought process was pretty much something along those lines... thanks.
    – Mohd Abdul Mujib
    Nov 11 at 1:29













up vote
1
down vote










up vote
1
down vote









Try using trim since a white space could be treated as a part of the text.



if (($(response).text().trim()) != ($(innerLevelHTML).text().trim())) {
...


One alternative approach I can think of, off of my head is, to set a hash inside a data attribute say data-hash="" whenever you set the html of that div, and then whenever you have a new ajax response you can generate a hash on the fly and compare that to the existing one and if a change is found just update the html and the hash value for that attribute.



This process minimises the confusion that may be caused due to dom being updated by some script.



Also, Instead of



innerLevelHTML.html("");
innerLevelHTML.append(response);


Why not just just set the html directly as



innerLevelHTML.html(response);





share|improve this answer














Try using trim since a white space could be treated as a part of the text.



if (($(response).text().trim()) != ($(innerLevelHTML).text().trim())) {
...


One alternative approach I can think of, off of my head is, to set a hash inside a data attribute say data-hash="" whenever you set the html of that div, and then whenever you have a new ajax response you can generate a hash on the fly and compare that to the existing one and if a change is found just update the html and the hash value for that attribute.



This process minimises the confusion that may be caused due to dom being updated by some script.



Also, Instead of



innerLevelHTML.html("");
innerLevelHTML.append(response);


Why not just just set the html directly as



innerLevelHTML.html(response);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 1:18

























answered Nov 11 at 0:55









Mohd Abdul Mujib

6,03043658




6,03043658








  • 1




    Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery function jQuery(...) or $(...) it will create a DOM Element for you, to test this do $("<div></div>"); while I do agree with your answers main point about using .html() it's cleaner and more efficient to just overnight the content once you have the text in memory already. so please remove your first line as it's not true.
    – Martin Barker
    Nov 11 at 1:03












  • Ahh it does work...My bad, Updated my answer. I don't know how I missed that. :)
    – Mohd Abdul Mujib
    Nov 11 at 1:12










  • I think I now know why you missed it, because the title is wrong and suggest that the OP is using .text on a jqXHR when they are not.
    – Martin Barker
    Nov 11 at 1:26












  • He he, on point, my thought process was pretty much something along those lines... thanks.
    – Mohd Abdul Mujib
    Nov 11 at 1:29














  • 1




    Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery function jQuery(...) or $(...) it will create a DOM Element for you, to test this do $("<div></div>"); while I do agree with your answers main point about using .html() it's cleaner and more efficient to just overnight the content once you have the text in memory already. so please remove your first line as it's not true.
    – Martin Barker
    Nov 11 at 1:03












  • Ahh it does work...My bad, Updated my answer. I don't know how I missed that. :)
    – Mohd Abdul Mujib
    Nov 11 at 1:12










  • I think I now know why you missed it, because the title is wrong and suggest that the OP is using .text on a jqXHR when they are not.
    – Martin Barker
    Nov 11 at 1:26












  • He he, on point, my thought process was pretty much something along those lines... thanks.
    – Mohd Abdul Mujib
    Nov 11 at 1:29








1




1




Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery function jQuery(...) or $(...) it will create a DOM Element for you, to test this do $("<div></div>"); while I do agree with your answers main point about using .html() it's cleaner and more efficient to just overnight the content once you have the text in memory already. so please remove your first line as it's not true.
– Martin Barker
Nov 11 at 1:03






Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery function jQuery(...) or $(...) it will create a DOM Element for you, to test this do $("<div></div>"); while I do agree with your answers main point about using .html() it's cleaner and more efficient to just overnight the content once you have the text in memory already. so please remove your first line as it's not true.
– Martin Barker
Nov 11 at 1:03














Ahh it does work...My bad, Updated my answer. I don't know how I missed that. :)
– Mohd Abdul Mujib
Nov 11 at 1:12




Ahh it does work...My bad, Updated my answer. I don't know how I missed that. :)
– Mohd Abdul Mujib
Nov 11 at 1:12












I think I now know why you missed it, because the title is wrong and suggest that the OP is using .text on a jqXHR when they are not.
– Martin Barker
Nov 11 at 1:26






I think I now know why you missed it, because the title is wrong and suggest that the OP is using .text on a jqXHR when they are not.
– Martin Barker
Nov 11 at 1:26














He he, on point, my thought process was pretty much something along those lines... thanks.
– Mohd Abdul Mujib
Nov 11 at 1:29




He he, on point, my thought process was pretty much something along those lines... thanks.
– Mohd Abdul Mujib
Nov 11 at 1:29


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244510%2fjquery-ajax-request-wrapped-in-jquerys-text-isnt-the-same-as-the-divs-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation