How to select each node that contains the same identifier

I have a Jstree that contains many nodes, some of them have the same ID.

I was wondering how can I make sure that if someone selects one of the nodes, he will select each node with the same identifier.

I tried to work with

    onselect: function (node) {

but I'm not sure what to do,
plus I'm not sure how to manually select node
(because everything is done with the selected attribute)

+3
source share
2 answers

, , , " - . , .

, , ; - :

var theTargetID = /* ...whatever ID you're looking for... */;
$(theTree).find("*").each(function(element) {
    if (this.id == theTargetID) {
        // it matches the ID
    }
});

( ). , DOM, jQuery, - ( ).

DOM, :

function traverse(theTargetID, element) {
    var node;

    if (element.id == theTargetID) {
        // It matches, do something about it
    }

    // Process child nodes
    for (node = element.firstChild; node; node = node.nextSibling) {
        if (node.nodeType === 1) {  // 1 == Element
            traverse(theTargetID, node);
        }
    }
}

, element DOM ( jQuery node ..). id, , . .

, node, . , node β€” .

+3

A T.J Crowder , . , jsTree, , .

node, , id var nodeId . var nodeId. , , node, , . , i .

, . (HTML Javascript), .

var nodeId = 'the-node-id'; // The id of your node id here.
$('#' + nodeId).each(function() {
  var matchingIds = $('[id='+this.id+']'); // May find duplicate ids.
  if (matchingIds.length > 1 && matchingIds[0] == this) {
    // Duplicates found.
    for (i = 0; i < matchingIds.length; i++) {
      // Whatever you like to do with the duplicates goes here. I suggest you give them new unique ids.
    }
  }
});

: , , T.J Crowder.

$('[id]').each(function() { // Selects all elements with ids in the document.
  var matchingIds = $('[id='+this.id+']'); // May find duplicate ids.
  if (matchingIds.length > 1 && matchingIds[0] == this) {
    // Duplicates found.
    for (i = 0; i < matchingIds.length; i++) {
      // Whatever you like to do with the duplicates goes here. I suggest you give them new unique ids.
    }
  }
});
0

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


All Articles