0

I have 7 row and i need to use my function only for row which was edited. How can i send id? or maybe there are more easy way? I tried to send number of row id onChange="setprice(this.value,18,5)" last one is ID of row, but on split int+ string I can not detetct it as ID . For example: var idofrow = ('setvalue'+row); $("#idofrow").html("");. #idofrow does not work

function setprice(val, ay, row) {
  var rowid_ilk = event.target.getAttribute('id');
  $("#rowid_ilk").html("");
  var total = Math.round((((({{ price_without_symbol }  } - val) / ay) * ({{      kredittable.kredit_faiz / 100 }})) + (({{ price_without_symbol }} - val) / ay)));
  var rowset = 'setvalue' + row;
  alert(rowset);
  rowset.innerHTML += total;
}
<table class="table table-striped" id="tablebrd">
  <thead>
    <tr>
      <th scope="col">{{ text_kredit_mesac }}</th>
      <th scope="col" style="width: 28%">{{ text_min_vznos }}</th>
      <th scope="col">{{ text_mes_plata }}</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>3 {{ text_kredit_mesaca }}</td>
      <td><input type="number" name="ilkoden" id="ilkoden1" value="{{ kredittable.kredit_min_ilkin  }}" onChange="setprice(this.value,3,1)" /></td>
      <td>
        <div id="setvalue1"></div>
      </td>
    </tr>
    <tr>
      <td>6 {{ text_kredit_mesaca }}</td>
      <td><input type="number" name="ilkoden" id="ilkoden2" value="{{ kredittable.kredit_min_ilkin  }}" onChange="setprice(this.value,6,2)" /></td>
      <td>
        <div id="setvalue2"></div>
      </td>
    </tr>

    <tr>
      <td>9 {{ text_kredit_mesaca }}</td>
      <td><input type="number" name="ilkoden" id="ilkoden3" value="{{ kredittable.kredit_min_ilkin  }}" onChange="setprice(this.value,9,3)" /></td>
      <td>
        <div id="setvalue3"></div>
      </td>
    </tr>

    <tr>
      <td>12 {{ text_kredit_mesaca }}</td>
      <td><input type="number" name="ilkoden" id="ilkoden4" value="{{ kredittable.kredit_min_ilkin  }}" onChange="setprice(this.value,12,4)" /></td>
      <td>
        <div id="setvalue4"></div>
      </td>
    </tr>

    <tr>
      <td>18 {{ text_kredit_mesaca }}</td>
      <td><input type="number" name="ilkoden" id="ilkoden5" value="{{ kredittable.kredit_min_ilkin  }}" onChange="setprice(this.value,18,5)" /></td>
      <td>
        <div id="setvalue5"></div>
      </td>
    </tr>

    <tr>
      <td>24 {{ text_kredit_mesaca }}</td>
      <td><input type="number" name="ilkoden" id="ilkoden6" value="{{ kredittable.kredit_min_ilkin  }}" onChange="setprice(this.value,24,6)" /></td>
      <td>
        <div id="setvalue6"></div>
      </td>
    </tr>

    <tr>
      <td>36 {{ text_kredit_mesaca }}</td>
      <td><input type="number" name="ilkoden" id="ilkoden7" value="{{ kredittable.kredit_min_ilkin  }}" onChange="setprice(this.value,36,7)" /></td>
      <td>
        <div id="setvalue7"></div>
      </td>
    </tr>
  </tbody>
</table>
Orik00
  • 73
  • 1
  • 9

1 Answers1

1

You can access the id of your changed element by accessing the target property of the event: event.target:

event.target is a reference to the object that dispatched the event

Then access its id name with .getAttribute. And remove the first seven letters of the id name to get the element's number with the method .substr.

event.target.getAttribute('id').substr(7)

Here's a quick demo:

const fn = function() {
  console.log(event.target.getAttribute('id').substr(7))
}
<div id="ilkoden11" onclick="fn()">This is item 11</div>

If you want to select the id of an element on the same level, you will need to do some navigating inside the HTML tree, the following selector will do that:

const div = event.target.closest('tr').querySelector('div').id;
                              ↑                  ↑
                        parent <tr>      find <div> child

Here is an example:

const setprice = function() {
  const target = event.target.id;
  const value = event.target.value;
  const div = event.target.closest('tr').querySelector('div').id;
  console.log(target, value, div)
}
<table>
<tr>
  <td><input id="ilkoden2" onkeyup="setprice()" /></td>
  <td>
    <div id="setvalue2"></div>
  </td>
</tr>
</table>
Ivan
  • 34,531
  • 8
  • 55
  • 100
  • Dear Ivan,thank you for reply. In this case i can send 1 ID only, but what to do with second ID of div? I am getting value of first input and use it in formula to get result. This result by innerHTML i am adding to DIV with another ID. P.S. I can not join ID(int) with string and write it as ID `#string_plus_int` – Orik00 Jun 19 '18 at 18:57
  • Are you using some kind of library? – Ivan Jun 19 '18 at 18:59
  • Why do you have those curly brackets `{` and `}`? – Ivan Jun 19 '18 at 19:06
  • @Ivan - Those look like either [Liquid](https://stackoverflow.com/questions/3330979/outputting-literal-curly-braces-in-liquid-templates) or [Angular](https://stackoverflow.com/questions/17878560/difference-between-double-and-single-curly-brace-in-angular-js) braces.... those are the "variable" braces, so Angular / Liquid (or whatever language) will render the contents of those variables. I'm hoping it's _not_ angular, since OP is using a lot of jQuery, which shouldn't be necessary / used in an Angular app :) – random_user_name Jun 19 '18 at 19:14
  • It is MVC php on opencart 3.0.2.0 version. `{{ variable }}` it is same thing as `` which i should pass and define in controller, model, language and finally in view – Orik00 Jun 19 '18 at 19:19
  • Thank you @cale_b, Orik00 I have edited my answer to allow the selection of the `div`'s id. – Ivan Jun 19 '18 at 19:33
  • @cale_b yes,exactly as "Liquid. It came with 3-rd version, old version is more comfortable i think, loops same as PHP. – Orik00 Jun 19 '18 at 19:52
  • @Orik00 did you try my second snippet? – Ivan Jun 19 '18 at 19:52
  • @Ivan just tested and it is perfect, thank you very much, you are awesome!) – Orik00 Jun 19 '18 at 20:00
  • I'm glad I could help ;) – Ivan Jun 19 '18 at 20:00
  • @Ivan I still have problem. If i write`div.innerHTML += total;` it is not working, but if i write `setvalue2.innertHTML += total;` it works. Why? i thought your code is solved this problem, but still same – Orik00 Jun 19 '18 at 20:15
  • `total` is an Integer, what do you want to do with it? Do you want to sum values, assign `total` or add `total` content to the page? – Ivan Jun 19 '18 at 20:23
  • @Ivan I have 7 rows with 3 columns(**1-st column we need for formula**). `target ` - is id of editable input(int). `value` - is value of `target`(**2-nd column**). `div`- is id of DIV where we should print `total`(**3-rd column**). `div.innerHTML += total;` div is not working here, does not show id of div to print it – Orik00 Jun 19 '18 at 20:29
  • @Ivan my stupid mistake... i should use ` document.getElementById(div).innerHTML += total;` to make it see at as ID. Thank you very much for help!) – Orik00 Jun 19 '18 at 20:35
  • 2
    Indeed, or you can set the element in the variable instead: `const div = event.target.closest('tr').querySelector('div')`. So then you can access its id with `div.id` and add some HTML with `div.innerHTML +=`. Though you should take a look at [this other question](https://stackoverflow.com/questions/11515383/why-is-element-innerhtml-bad-code) which explains why adding HTML with `.innerHTML +=` is not very good. Don't worry, for now, since you're starting off in JavaScript you don't have to worry about that ;) – Ivan Jun 19 '18 at 20:45
  • @Ivan Thank you for help and for documentations, i will read and will try to fix it :) – Orik00 Jun 19 '18 at 21:22