-5

Good day! I want to increment a subscript in a div it name, like "#jstree1", "#jstree2"... etc. I know iterations and incrementing values are a solution. But with this kind of structure, I can't figure out how.

<script src= '@routes.Assets.at("dist/libs/jquery.js")' type="text/javascript"></script>
<script src= '@routes.Assets.at("dist/jstree.min.js")' type="text/javascript"></script>
<script>
  $(function () {
    var i = 0; 

    // 6 create an instance when the DOM is ready
    $('#jstree'+i).jstree({
          "core" : {
                "animation" : 0,
                "check_callback" : true,
                "themes" : { "stripes" : true }
              },
              /*"plugins" : [
                "search", "unique",
                "state", "types"
              ]*/
              "plugins" : [
                            "contextmenu", "dnd", "search",
                            "state", "types", "core", "json_data", "ui", "crrm"
                          ]   
    });

    i++;

    var to = false;
    var k = 0;
    $('#plugins4_q').keyup(function () {
      if(to) { clearTimeout(to); }
      to = setTimeout(function () {     
        var v = $('#plugins4_q').val();
        $('#jstree'+k).jstree(true).search(v);
      }, 250);      
    });
    k++;

  });

  </script>

  }

I need to increment the div jstree for displaying the tree and making the search bar to work but it doesn't work. So I'm thinking to assign the variable to any number, if possible. Please help me fix this. Thank you very much.

Spundun
  • 3,936
  • 2
  • 23
  • 36
user3583667
  • 51
  • 2
  • 11
  • 1
    It's unclear to me what you are asking for. A number is a data type in JS. You can't assign a variable to a data type. – Felix Kling Jun 02 '14 at 03:03
  • You can't assign a variable to a number. That'd look something like `1 = x;` and that doesn't make any sense. And of course you can assign a number to a variable, that'd look like `x=1;`. But I don't think that's what you are looking for. You need to think more about what is it that you are asking. Put yourself in our shoes and think how your question will read to another person. More importantly think about how you can modify it so it'll help us answer it. – Spundun Jun 02 '14 at 03:04
  • I don't see how the code you've provided is even remotely descriptive of the question you're asking. Try distilling your code down to the bare minimum of what demonstrates the issue you're having. – monners Jun 02 '14 at 03:05
  • I'm guessing he means the other way around. In which case, the answer is yes. Of course you can. – Meredith Jun 02 '14 at 03:06
  • I'm sorry it's the other way around. That should be assigning a number to a variable. – user3583667 Jun 02 '14 at 03:06
  • By the way you are already "assigning a number to a variable" in your code. `var i = 0;` – Spundun Jun 02 '14 at 03:07
  • I mean, assigning any number to a variable. That is, any number (0, 1, 2, ...) should work for a variable. – user3583667 Jun 02 '14 at 03:09
  • Yes, you can assign any number to a variable and it works the same way as if you assign `0`. Example: `var i = 7246213548;` – Felix Kling Jun 02 '14 at 03:09
  • Thanks but I was thinking to assign any number to a single variable. For example, 0, 1, 2, 3, and so on is true for variable `i` only. I tried incrementing values but it didn't work for me. – user3583667 Jun 02 '14 at 03:12
  • Again, you can assign any number to a variable, but you have to specify the value in some way. You can't have a "number placeholder value" if that's what you mean. – Felix Kling Jun 02 '14 at 03:14
  • Thanks! Perhaps you can help me with incrementing the values? I'm really having a hard time because ID's must have a unique name. – user3583667 Jun 02 '14 at 03:15
  • I mean in my code given above. I've been trying to increment the values but only the first set (or the last set) only works. – user3583667 Jun 02 '14 at 03:18
  • I recommend you reduce the code to the bare minimum that shows your problem and [edit] your question to provide a proper descriptions of your problem. – Felix Kling Jun 02 '14 at 03:22
  • @user3583667 You are obviously new to stackoverflow and looks like you are very new to javascript and coding. Don't be disheartened, keep on reading about javascript and Felix Kling is giving the right advice. Try to make a smaller code example to show us what you are looking for. – Spundun Jun 02 '14 at 03:26
  • @user3583667 Are you asking to assign **all** the numbers to the same variable at the same time? – Spundun Jun 02 '14 at 03:26
  • "So I'm thinking to assign the variable to any number" => Which variable? – Spundun Jun 02 '14 at 03:27
  • Since the div id `jstree+i` wherein `i` increments, I also need to increment `jstree` in the javascript. – user3583667 Jun 02 '14 at 03:30
  • In both `$('#jstree').jstree({...` and `$('#plugins4_q').keyup(function () {...`. Thanks. – user3583667 Jun 02 '14 at 03:31
  • Can you explain what you mean by "increment jstree" ? jstree doesn't seem to be a variable in javascript. – Spundun Jun 02 '14 at 03:57
  • @user3583667 Also, I suggest you go to this chat room... http://chat.stackoverflow.com/rooms/17/javascript Explain that you are very new to coding and javascript and patiently, explain things. I think you have a better chance of getting help there... or try irc. http://irchelp.org/irchelp/irctutorial.html . irc chatroom #javascript on freenode network. – Spundun Jun 02 '14 at 04:00
  • I used it as a div id. I need to increment it since ID's must be unique. My problem is I can't increment it properly. Btw, much as I would like to join the chat room, I can't since I lack reputation. Thanks. – user3583667 Jun 02 '14 at 04:00
  • So you need to explain to us, on which line of code you want to increment the div id, and you also need to explain what you mean by incrementing. Because a div id is not a number, it's a string... the idea of incrementing refers to numbers while strings don't work that way... http://en.wikipedia.org/wiki/String_(computer_science) – Spundun Jun 02 '14 at 04:03
  • http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(string_functions) – Spundun Jun 02 '14 at 04:05
  • Thanks. Yes, it is actually a string with a number appended in it. I can increment the div id. My problem is that if you'll look at this line `$('#jstree'+i).jstree({...` (javascript is really my problem), I'm incrementing `i` below but jstree (http://www.jstree.com/) doesn't work in all the `div`s, only in the first one since it is not incremented properly. – user3583667 Jun 02 '14 at 04:08
  • `$('#jstree'+i).jstree(...)` is only called **once**. Incrementing `i` doesn't have any effect since you are not using `i` anymore after you incremented it. It looks like you want to select all elements that have an ID **starting** with `jstree`. You can do this with the "attribute starts with selector": `$('[id=^jstree]').jstree(...)`. http://api.jquery.com/attribute-starts-with-selector/ . Or simpler, give all those elements the same class, and use `$('.className').jstree(...)` instead. – Felix Kling Jun 02 '14 at 04:11
  • So to be clear you want to make div ids like "#jstree1", "#jstree2"... etc? – Spundun Jun 02 '14 at 04:17
  • Thanks @FelixKling. Classname worked for the display of the tree `.jstree(...)` but it didn't in the search so I used the "attribute starts with selector" `$("[id^='jstree']").jstree(true).search(v);` but the search only works for the first `div`. – user3583667 Jun 02 '14 at 04:28
  • You probably have to iterate over all "jstrees" using https://api.jquery.com/each/ and search each of them individually. – Felix Kling Jun 02 '14 at 04:53

2 Answers2

0

Ummm.... Yes! In Javascript any number can be assigned to a variable.

i = 10;
x = 1.5;

... etc.

Spundun
  • 3,936
  • 2
  • 23
  • 36
-2

Yes you can assign any numbers but when you doing some logical operation make sure to use strict notations.

===/!== 

because following bugs can be happened

var number = 1.2;
if(number == "1.2") == > TRUE
if(number == 1.2) == > TRUE
if(number == 1) ==> TRUE
Mudeyanse
  • 50
  • 5
  • `1.2 == 1` is `false`. I also fail to see how this is relevant to question (not that the question makes a lot of sense). – Felix Kling Jun 02 '14 at 03:15
  • it is example of using numbers and logical operation, JavaScript some time take 1.2 as 1. – Mudeyanse Jun 02 '14 at 03:28
  • Uhm no. `1.2` is always `1.2`, not `1`. The value of a number doesn't change magically. Sometimes you have *precision issues*, e.g. with `0.2 + 0.1`, but that's something different. And it doesn't change the fact that your claim that `1.2 == 1` is `true` is wrong. – Felix Kling Jun 02 '14 at 03:29
  • Ok Thank you very much try to use var x = "1.2"; if(x==1){alert("in");} – Mudeyanse Jun 02 '14 at 03:36
  • `"1.2" == 1` is still false, so the alert doesn't show. – Felix Kling Jun 02 '14 at 03:48
  • var x= "1.2"; if(x===1.2){ alert("in");} – Mudeyanse Jun 02 '14 at 03:59
  • Yes, `"1.2" == 1.2` is `true`, which is solved by using strict comparison. But that has nothing to do with comparing `"1.2"` or `1.2` with `1`, which is would you did in your answer and comments. And all of that has nothing to do with the problem the OP is having. – Felix Kling Jun 02 '14 at 04:05