Anki Forums

How can I hide a 2nd field if it matches with the 1st field?

Hi y’all,

I have two fields that sometimes bear complimentary information but in some instances they match.
I would like to hide the 2nd field if it matches with the 1st one.

This is the code I could come up with:

<div class="field1"><span>verb</span> regarder</div>
<div class="field2">regarder <span>qn</span></div>
<script>
var removeElements = function(text, selector) {
    var wrapped = $("<div>" + text + "</div>");
    wrapped.find(selector).remove();    
    return wrapped.html();
}

var removedSpanStringFlash5 = removeElements($('.field1').html(), "span").trim();
var removedSpanStringFlash6 = removeElements($('.field2').html(), "span").trim();

if(removedSpanStringFlash5 == removedSpanStringFlash6) {
$('.field2').hide();
}

</script>

In this case I would only see field1 on the card.

I would also like to hide the 2nd field if it contains information in parenthesis that need to be removed before executing the match, example:

<div class="field1"><span>verb</span> regarder</div>
<div class="field2">regarder <span>qn</span> (verb)</div>

I tried to integrate the following code snippets in the code above to fulfill this task but I didn’t succeed:
code snippet 1:

$('.field1').text(function(_, text) {
    return text.replace(/\s\(.*?\)/g, '');
});

code snippet 2:

function stripParenthesis( node ) {
    if(node.length) {
        node.contents().each(function(index, child) {
            if( child.nodeType === 3 ) {
                child.nodeValue = child.nodeValue.replace(/\(.*?\)/g, '');
            }
            else {
                stripParenthesis( $(child) );
            }
        });
    }
}

stripParenthesis( $('.field1') );

Does anyone know how to make the magic work?
Thanks a lot!
Cheers! :slight_smile: