How to find html elements containing specific text in an html comment?

I am trying to use jQuery to identify <td> elements containing special HTML comment text. The standard ": contains" selector does not seem to look in the comment. My context here is that I want to dynamically add some content to the same td element. Here's a sample target td:

<TD valign="top" class="ms-formbody" width="400px">
        <!-- FieldName="Title"
             FieldInternalName="Title"
             FieldType="SPFieldText"
          -->
            <span dir="none">
        <input name="ctl00$m$g_0b1e4ca3_9450_4f3e_b3af_8134fe014ea5$ctl00$ctl04$ctl00$ctl00$ctl00$ctl04$ctl00$ctl00$TextField" type="text" maxlength="255" id="ctl00_m_g_0b1e4ca3_9450_4f3e_b3af_8134fe014ea5_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField" title="Title" class="ms-long" /><br>
    </span>


        </TD>

This html is being created by SharePoint as part of the new form of the list item, and I really want to find td in my document whose "FieldInternalName" in the comment matches some text string. How can I use jQuery (or is any javascript really okay) to best find these td elements?

+3
2

innerHTML .

:

// HTML: <div id="myElement">Lorem <!-- a comment --> ipsum.</div>

var element = document.getElementById('myElement');
alert(element.innerHTML); // alerts "Lorem <!-- a comment --> ipsum."
// here you could run a regular expression

: ( jQuery):

var tds = $('td'), match;
tds.each(function(index, element) {
    match = $(element).innerHTML.match(/FieldInternalName="(^["])"/);
    if (match) alert(match);
});
+3
if(document.getElementById('myElement').innerHTML.indexOf(text_string) != -1) { //do something }
0

Source: https://habr.com/ru/post/1704220/


All Articles