how to get last comment in dynamically changed html with js or jquery?










1















i'm trying to find and alter the last comment node in a page (which will change periodically).



something like



<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>


the element following the last comment will always be "something", maybe there's a way to just grab the node before "something"? having a hard time unpacking how dealing with nodes works



edit: i've found answers about finding all the comment nodes but can't figure out how to select only the last one. an additional hazard is that this is a weird project where i need to specifically use custom elements without ids or classes










share|improve this question
























  • Possible duplicate of Selecting HTML comments with jQuery

    – Tyler Roper
    Nov 13 '18 at 21:55











  • Can you improve your html markup with the comments structure?

    – Shidersz
    Nov 13 '18 at 22:00











  • @TylerRoper this is for a weird project with special crazy rules so i'm trying to avoid plugins though that looks like a good way. i'm currently using the "return this.nodeType == 8;" technique in other parts of my code but can't figure out how to use that to select only the last comment

    – milpool
    Nov 13 '18 at 22:46















1















i'm trying to find and alter the last comment node in a page (which will change periodically).



something like



<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>


the element following the last comment will always be "something", maybe there's a way to just grab the node before "something"? having a hard time unpacking how dealing with nodes works



edit: i've found answers about finding all the comment nodes but can't figure out how to select only the last one. an additional hazard is that this is a weird project where i need to specifically use custom elements without ids or classes










share|improve this question
























  • Possible duplicate of Selecting HTML comments with jQuery

    – Tyler Roper
    Nov 13 '18 at 21:55











  • Can you improve your html markup with the comments structure?

    – Shidersz
    Nov 13 '18 at 22:00











  • @TylerRoper this is for a weird project with special crazy rules so i'm trying to avoid plugins though that looks like a good way. i'm currently using the "return this.nodeType == 8;" technique in other parts of my code but can't figure out how to use that to select only the last comment

    – milpool
    Nov 13 '18 at 22:46













1












1








1


0






i'm trying to find and alter the last comment node in a page (which will change periodically).



something like



<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>


the element following the last comment will always be "something", maybe there's a way to just grab the node before "something"? having a hard time unpacking how dealing with nodes works



edit: i've found answers about finding all the comment nodes but can't figure out how to select only the last one. an additional hazard is that this is a weird project where i need to specifically use custom elements without ids or classes










share|improve this question
















i'm trying to find and alter the last comment node in a page (which will change periodically).



something like



<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>


the element following the last comment will always be "something", maybe there's a way to just grab the node before "something"? having a hard time unpacking how dealing with nodes works



edit: i've found answers about finding all the comment nodes but can't figure out how to select only the last one. an additional hazard is that this is a weird project where i need to specifically use custom elements without ids or classes







javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 22:52







milpool

















asked Nov 13 '18 at 21:52









milpoolmilpool

189114




189114












  • Possible duplicate of Selecting HTML comments with jQuery

    – Tyler Roper
    Nov 13 '18 at 21:55











  • Can you improve your html markup with the comments structure?

    – Shidersz
    Nov 13 '18 at 22:00











  • @TylerRoper this is for a weird project with special crazy rules so i'm trying to avoid plugins though that looks like a good way. i'm currently using the "return this.nodeType == 8;" technique in other parts of my code but can't figure out how to use that to select only the last comment

    – milpool
    Nov 13 '18 at 22:46

















  • Possible duplicate of Selecting HTML comments with jQuery

    – Tyler Roper
    Nov 13 '18 at 21:55











  • Can you improve your html markup with the comments structure?

    – Shidersz
    Nov 13 '18 at 22:00











  • @TylerRoper this is for a weird project with special crazy rules so i'm trying to avoid plugins though that looks like a good way. i'm currently using the "return this.nodeType == 8;" technique in other parts of my code but can't figure out how to use that to select only the last comment

    – milpool
    Nov 13 '18 at 22:46
















Possible duplicate of Selecting HTML comments with jQuery

– Tyler Roper
Nov 13 '18 at 21:55





Possible duplicate of Selecting HTML comments with jQuery

– Tyler Roper
Nov 13 '18 at 21:55













Can you improve your html markup with the comments structure?

– Shidersz
Nov 13 '18 at 22:00





Can you improve your html markup with the comments structure?

– Shidersz
Nov 13 '18 at 22:00













@TylerRoper this is for a weird project with special crazy rules so i'm trying to avoid plugins though that looks like a good way. i'm currently using the "return this.nodeType == 8;" technique in other parts of my code but can't figure out how to use that to select only the last comment

– milpool
Nov 13 '18 at 22:46





@TylerRoper this is for a weird project with special crazy rules so i'm trying to avoid plugins though that looks like a good way. i'm currently using the "return this.nodeType == 8;" technique in other parts of my code but can't figure out how to use that to select only the last comment

– milpool
Nov 13 '18 at 22:46












1 Answer
1






active

oldest

votes


















1














This is interesting, and I'm not sure if this is the best method, but if you can count on it always being before a certain DOM element, then you could start at that element and work backwards until you find the first comment. See this code as an example.






let something = document.getElementsByTagName("something")[0]
let currentNode = something

// 8 is the node type for a comment
while (currentNode !== null && currentNode.nodeType !== 8)
currentNode = currentNode.previousSibling


console.log(currentNode.data)

<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>








share|improve this answer

























  • ooh this is very close!! but i'm doing a weird project where i can't use ids just dom elements like <something></something> and i can't figure out how to alter this code to work that way. ideas?

    – milpool
    Nov 13 '18 at 22:43






  • 1





    Well, if you have to use "something" you can select by tag name, and then get the first element. I updated my answer.

    – Jordan S
    Nov 13 '18 at 23:03











  • YES this is perfect thank you!! i'm new to pure js and didn't know about selecting by tag names, that's great!

    – milpool
    Nov 13 '18 at 23:12










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',
autoActivateHeartbeat: false,
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%2f53290066%2fhow-to-get-last-comment-in-dynamically-changed-html-with-js-or-jquery%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









1














This is interesting, and I'm not sure if this is the best method, but if you can count on it always being before a certain DOM element, then you could start at that element and work backwards until you find the first comment. See this code as an example.






let something = document.getElementsByTagName("something")[0]
let currentNode = something

// 8 is the node type for a comment
while (currentNode !== null && currentNode.nodeType !== 8)
currentNode = currentNode.previousSibling


console.log(currentNode.data)

<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>








share|improve this answer

























  • ooh this is very close!! but i'm doing a weird project where i can't use ids just dom elements like <something></something> and i can't figure out how to alter this code to work that way. ideas?

    – milpool
    Nov 13 '18 at 22:43






  • 1





    Well, if you have to use "something" you can select by tag name, and then get the first element. I updated my answer.

    – Jordan S
    Nov 13 '18 at 23:03











  • YES this is perfect thank you!! i'm new to pure js and didn't know about selecting by tag names, that's great!

    – milpool
    Nov 13 '18 at 23:12















1














This is interesting, and I'm not sure if this is the best method, but if you can count on it always being before a certain DOM element, then you could start at that element and work backwards until you find the first comment. See this code as an example.






let something = document.getElementsByTagName("something")[0]
let currentNode = something

// 8 is the node type for a comment
while (currentNode !== null && currentNode.nodeType !== 8)
currentNode = currentNode.previousSibling


console.log(currentNode.data)

<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>








share|improve this answer

























  • ooh this is very close!! but i'm doing a weird project where i can't use ids just dom elements like <something></something> and i can't figure out how to alter this code to work that way. ideas?

    – milpool
    Nov 13 '18 at 22:43






  • 1





    Well, if you have to use "something" you can select by tag name, and then get the first element. I updated my answer.

    – Jordan S
    Nov 13 '18 at 23:03











  • YES this is perfect thank you!! i'm new to pure js and didn't know about selecting by tag names, that's great!

    – milpool
    Nov 13 '18 at 23:12













1












1








1







This is interesting, and I'm not sure if this is the best method, but if you can count on it always being before a certain DOM element, then you could start at that element and work backwards until you find the first comment. See this code as an example.






let something = document.getElementsByTagName("something")[0]
let currentNode = something

// 8 is the node type for a comment
while (currentNode !== null && currentNode.nodeType !== 8)
currentNode = currentNode.previousSibling


console.log(currentNode.data)

<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>








share|improve this answer















This is interesting, and I'm not sure if this is the best method, but if you can count on it always being before a certain DOM element, then you could start at that element and work backwards until you find the first comment. See this code as an example.






let something = document.getElementsByTagName("something")[0]
let currentNode = something

// 8 is the node type for a comment
while (currentNode !== null && currentNode.nodeType !== 8)
currentNode = currentNode.previousSibling


console.log(currentNode.data)

<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>








let something = document.getElementsByTagName("something")[0]
let currentNode = something

// 8 is the node type for a comment
while (currentNode !== null && currentNode.nodeType !== 8)
currentNode = currentNode.previousSibling


console.log(currentNode.data)

<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>





let something = document.getElementsByTagName("something")[0]
let currentNode = something

// 8 is the node type for a comment
while (currentNode !== null && currentNode.nodeType !== 8)
currentNode = currentNode.previousSibling


console.log(currentNode.data)

<!-- first comment -->
<!-- second comment -->
<!-- third comment -->
<something></something>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 23:03

























answered Nov 13 '18 at 22:05









Jordan SJordan S

3,0691021




3,0691021












  • ooh this is very close!! but i'm doing a weird project where i can't use ids just dom elements like <something></something> and i can't figure out how to alter this code to work that way. ideas?

    – milpool
    Nov 13 '18 at 22:43






  • 1





    Well, if you have to use "something" you can select by tag name, and then get the first element. I updated my answer.

    – Jordan S
    Nov 13 '18 at 23:03











  • YES this is perfect thank you!! i'm new to pure js and didn't know about selecting by tag names, that's great!

    – milpool
    Nov 13 '18 at 23:12

















  • ooh this is very close!! but i'm doing a weird project where i can't use ids just dom elements like <something></something> and i can't figure out how to alter this code to work that way. ideas?

    – milpool
    Nov 13 '18 at 22:43






  • 1





    Well, if you have to use "something" you can select by tag name, and then get the first element. I updated my answer.

    – Jordan S
    Nov 13 '18 at 23:03











  • YES this is perfect thank you!! i'm new to pure js and didn't know about selecting by tag names, that's great!

    – milpool
    Nov 13 '18 at 23:12
















ooh this is very close!! but i'm doing a weird project where i can't use ids just dom elements like <something></something> and i can't figure out how to alter this code to work that way. ideas?

– milpool
Nov 13 '18 at 22:43





ooh this is very close!! but i'm doing a weird project where i can't use ids just dom elements like <something></something> and i can't figure out how to alter this code to work that way. ideas?

– milpool
Nov 13 '18 at 22:43




1




1





Well, if you have to use "something" you can select by tag name, and then get the first element. I updated my answer.

– Jordan S
Nov 13 '18 at 23:03





Well, if you have to use "something" you can select by tag name, and then get the first element. I updated my answer.

– Jordan S
Nov 13 '18 at 23:03













YES this is perfect thank you!! i'm new to pure js and didn't know about selecting by tag names, that's great!

– milpool
Nov 13 '18 at 23:12





YES this is perfect thank you!! i'm new to pure js and didn't know about selecting by tag names, that's great!

– milpool
Nov 13 '18 at 23:12



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290066%2fhow-to-get-last-comment-in-dynamically-changed-html-with-js-or-jquery%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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo