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>
javascript php jquery
|
show 1 more comment
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>
javascript php jquery
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 becauserequest.text()
is a valid method for thefetch
API, and not valid for the jqXHR object it'sjQuery().text
onjqXHR response
does not match text already in HTML when it should
– Martin Barker
Nov 11 at 1:23
|
show 1 more comment
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>
javascript php jquery
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
javascript php jquery
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 becauserequest.text()
is a valid method for thefetch
API, and not valid for the jqXHR object it'sjQuery().text
onjqXHR response
does not match text already in HTML when it should
– Martin Barker
Nov 11 at 1:23
|
show 1 more comment
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 becauserequest.text()
is a valid method for thefetch
API, and not valid for the jqXHR object it'sjQuery().text
onjqXHR 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
|
show 1 more comment
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);
1
Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery functionjQuery(...)
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
add a comment |
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);
1
Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery functionjQuery(...)
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
add a comment |
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);
1
Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery functionjQuery(...)
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
add a comment |
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);
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);
edited Nov 11 at 1:18
answered Nov 11 at 0:55
Mohd Abdul Mujib
6,00543658
6,00543658
1
Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery functionjQuery(...)
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
add a comment |
1
Your answer's strapline is just wrong. from the offset if you put HTML text as a response is into a the jQuery functionjQuery(...)
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 thefetch
API, and not valid for the jqXHR object it'sjQuery().text
onjqXHR response
does not match text already in HTML when it should– Martin Barker
Nov 11 at 1:23