javascript - Compare 2 elements in jQuery (by content) - Stack Overflow
So, I have a page with messages. I'm using jQuery.load
(requesting the exact same page) to refresh the page smoothly once every X seconds.
The problem is that if there are images on the page, they get reloaded.
So basically what I want to do, is to still use the load
method, but to only update the changed elements.
Is it possible to pare jQuery('.message-wrapper').first()
with jQuery('.message-wrapper').last()
and if they have the exact same structure / content, it should return true. Right now if you pare 2 HTML nodes (so JS, not jQuery), you get true only if they are one and the same element.
What I want to do is check the content to see if it's different.
Disclaimer: I've seen a few similar questions, but none have a working solution.
So, I have a page with messages. I'm using jQuery.load
(requesting the exact same page) to refresh the page smoothly once every X seconds.
The problem is that if there are images on the page, they get reloaded.
So basically what I want to do, is to still use the load
method, but to only update the changed elements.
Is it possible to pare jQuery('.message-wrapper').first()
with jQuery('.message-wrapper').last()
and if they have the exact same structure / content, it should return true. Right now if you pare 2 HTML nodes (so JS, not jQuery), you get true only if they are one and the same element.
What I want to do is check the content to see if it's different.
Disclaimer: I've seen a few similar questions, but none have a working solution.
Share Improve this question edited May 18, 2013 at 0:06 Eduard Luca asked May 17, 2013 at 23:38 Eduard LucaEduard Luca 6,61218 gold badges88 silver badges142 bronze badges 6-
3
$('.message-wrapper').first().text() == $('.message-wrapper').last().text()
– adeneo Commented May 17, 2013 at 23:46 -
@adeneo if the have the exact same
structure
/ content, it should return true – sabithpocker Commented May 17, 2013 at 23:49 -
@adeneo if you'd be so kind to post that as an answer, I'd be glad to accept it. Now I'm ashamed I even asked this question.
.text()
... wow. – Eduard Luca Commented May 18, 2013 at 0:04 - @sabithpocker yes, I requested that, and adeneo's solution wouldn't cover it, but I can deal with that separately, as most of my cases have the same structure, it's just a few exceptions that don't. – Eduard Luca Commented May 18, 2013 at 0:05
-
1
You can use
.html()
instead of.text()
to account for both structure and content. – bfavaretto Commented May 18, 2013 at 0:28
1 Answer
Reset to default 4To pare the text content of your elements you can use text()
, or to pare the markup, use html()
:
$('.message-wrapper').first().text() == $('.message-wrapper').last().text()
To remove spaces before and after the string, which can be an issue sometimes, you can wrap it in jQuery's $.trim
method :
$.( $('.message-wrapper').first().html() ) === $.( $('.message-wrapper').last().html() )
- 下个月Win7正式“退休”,数据显示国内近60%电脑用户仍在使用
- 为何政府机关被禁止采购Win 8系统?
- 网络购票冲击下的铁路代售点被迫转型
- R CatBoost support for incremental training - Stack Overflow
- Issue with Ruby in CICD environment on Linux: usrbinenv: 'ruby.exe': No such file or directory - Stack Overflow
- ios - Flutter: works on Xcode but not on VS code and Android Studio - Stack Overflow
- ios - CocoaPods could not find compatible versions for pod "FirebaseCore": - Stack Overflow
- node.js - Cors errors S3 Upload node - Stack Overflow
- kotlin - I don't know how to get Integers from another function without calling the variables that hold said number - St
- Flutter - individual bloc states - Stack Overflow
- javascript - AJAX implementation doesn't work properly for "like" button ASP.NET Core 2.1 - Stack Over
- darkmode - Eclipse IDE - Dark Mode: When "Link With Editor" is enabled in project explorer the selected file&a
- i tried to get read edit manage storage in android java and output permission denied and not found in device setting - Stack Ove
- angular - Infer types in callback chain based on `null` return as success Indicator - Stack Overflow
- php - Laravel Defer on API requests - Stack Overflow
- asp.net - Delete Button does not delete in C# Webforms - Stack Overflow
- r - How to change the collapse breakpoints of a bslib navbar? - Stack Overflow