3

I would like to know if it is possible to capture a commented remark using JavaScript.

The remark will look like this on the soruce code:

<div id='ContainerId'>
<!-- NON IDEAL REASON: 90 min time window depart arrive -->
<b>1,107.45 GBP</b><br />
<!--LLF: 1107.45 -->
</div>

I need to save that value (in this case 1107.45) inside a variable.

This doesn't seem to work:

var LLF = jQuery("contains('LLF')");

Any ideas?

Thanks!

8

1 Answer 1

5
$('#ContainerId').contents().filter(function(){
    return this.nodeType === 8 // Comment node
});

Live DEMO

And the full code:

var comment = $('#ContainerId').contents().filter(function() {
    return this.nodeType === 8 // Comment node
})[0].nodeValue;

console.log(comment.match(/\d+\.\d+/g));​​​​​

Live DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I am looking for, thank you. Nevertheless, I realized that I have other commented lines under the same DIV, so it's capturing the first line instead of the desired one. How could I filter the node value? Edit: I have updated the source code to reflect the code in place.
Sorry for my lame comment, just realized this: [0].nodeValue;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.