tags:

views:

76

answers:

2

Hello, I have a Jstree that holds a lot of nodes, some of them have the same ID.

I was wondering, how do I make it so that if someone selects
one of the nodes, it would select every node with the same id.

I tried working with the

    onselect: function (node) {

but I'm not sure what exactly to do,
plus I'm not sure how to manually select a node
(because it's all done with the selected: attribute)

+3  A: 
T.J. Crowder
Some good ideas, but what if the duplicate ids exist on nodes who are ancestors to the node?
Erik Töyrä
@Erik: I'll update it, I assumed the OP was referring to the tree's node with `node`, but I suspect I was mistaken. Thanks.
T.J. Crowder
A: 

A T.J Crowder already have said, IDs must be unique within a document. I think you can end up with a very strange effect in your jsTree if there are duplicated IDs, so I would recommend that you do the following.

For each node you're clicking on, store the value of the id attribute in var nodeId in the example below. The example code will find duplicates of var nodeId for you. If you find duplicates, then all but the first found node should have the id changed to a unique id. You can do that by appending the value of i or a random text string to the id.

That's all I can do for you now. If you could provide us with some more detailed information (HTML and your current Javascript code) that would help.

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.
    }
  }
});

Update: This is an alternative solution where the duplicated ids are found directly after page load, similiar to T.J Crowder's suggestion.

$('[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.
    }
  }
});
Erik Töyrä