0

I've been trying to read a particular value from a row of display that I have on my php webpage. The idea is I am trying to get the current value of the certain cell in the row, however, I've tried using getElementsByTagName does not work, as I can not access the value afterwards (only has HTMLCollection object). I am using closest, but this only works and gets the beginning row.

EDITED:

Does not seem to pop up anything. Well the modal does pop up, but no alerts.

Tried this demo but did not work either haha...

<td><button class='sellButton btn btn-primary' data-target='#modalsell' id='sellStock'>Sell Stock</button></td>

<div id="modalsell" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title">Sell Stocks</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <form role="form">
                    <div class="form-group">
                        <div class="input-group">
                            <label for="shares" class="input-group-addon glyphicon glyphicon-user">Amount of Shares:</label>
                            <input type="text" class="form-control" id="sharess" placeholder="Shares Selling...">
                            <label for="start" class="input-group-addon glyphicon glyphicon-user">Minimal Sell Price:</label>
                            <input type="text" class="form-control" id="starts" placeholder="(0 for No Limit)" value="0">

                            <label for="stop" class="input-group-addon glyphicon glyphicon-user">Max Sell Price:</label>
                            <input type="text" class="form-control" id="stops" placeholder="(0 for No Limit)" value="0">
                        </div> <!-- /.input-group -->
                    </div> <!-- /.form-group -->

                </form>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-toggle='modal' onclick="sellStock()">Place Sell</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
              </div>
            </div>
          </div>
      </div>

undefined elements in invoker

$(document).ready(function(){
    $(".sellButton").on('click', function (e) {
       alert('test 1');
       $('#modalsell').modal('toggle', $(this));
    });
    $('#modalsell').on('show.bs.modal', function(button){
        alert('test 2');
        var $invoker = $(button.relatedTarget);
        alert($($invoker).id);
    });
});
Bao Thai
  • 533
  • 1
  • 6
  • 25
  • hmm I tried parentElement.parentNode and well, it works still only for the first row. However I have `document.getElementById('sellStock').parentElement.parentNode`,? – Bao Thai Apr 15 '17 at 06:50
  • Why can't you use $(this).closest("tr") for the button on the same row??? I see jQuery so USE it - also why TH in tbody??? – mplungjan Apr 15 '17 at 06:51
  • I was using bootstrap – Bao Thai Apr 15 '17 at 06:53
  • you do not have unique IDs either. There are some many issues with your code I do not even want to... – mplungjan Apr 15 '17 at 06:54
  • I mean that is what I mentioned, that's why I am asking is there a way to work around it since I am using a loop to obtain rows of info to place it in a table – Bao Thai Apr 15 '17 at 06:54
  • Yes, use classes where needed instead of ID. – mplungjan Apr 15 '17 at 07:16

3 Answers3

1

Jquery solution

First Case:

If you want whole row of table then you have to identify your table using some unique class or Id.

Lets say if your table unique class name is "table-responsive" then you can get all row of table using

$(".table-responsive").find('tbody tr');

It will give you rows of tbody.

Second case:

If you want to get row of current click button(in this case sellStock) button then you can use parents in context of this on button click.

Example:

From @Touheed Khan answer: Don't use id if you are generating multiple element in loop dynamically. you can use class, otherwise it will get only first element.

Let's say if your button class name is rowButton then you can use it as like below.

  $(".rowButton").click(function() {
         var currrentRow =$(this).parents('tr');
    });

thanks @Touheed Khan.

No one
  • 1,130
  • 1
  • 11
  • 21
  • Is there a way to include this somehow in a massive function? As the `var` statement above in included within a huge function I created. (not proficient programmer). Will this also works considering I have many buttons under the same ID? – Bao Thai Apr 15 '17 at 06:40
  • Can you give example or post your exact problem. Means I think you current problem (getting parent tr of click button is being solved by above code). what you mean by massive function? – No one Apr 15 '17 at 06:43
  • or do you want all the rows of table? – No one Apr 15 '17 at 06:46
1

Issue : the main issue with your code is you are using same id in loop (id='sellStock'). This is the reason you are getting same tr on clicking any button.

Note : id attribute should be unique inside page.

Solution : Try this code for getting parent tr of clicked button.

<th><button onclick="myFunction(this)">Sell Stock</button></th>

your handler function :

function myFunction(elem) {
    var x = elem.parentElement.parentElement; // x is the parent row 
    //rest of your code goes here
}

Alternative :

You can also use class selector instead of id which is a better option.

update as per comment request :

you can use get the invoker element of modal by doing this. #your-modal is your modal selector, you can use class or id as per your requirement.

$('#your-modal').on('show.bs.modal', function (e) {
  var $invoker = $(e.relatedTarget);
  //rest of code
});

instead of :

 alert($($invoker).id);

use this :

 alert($($invoker).attr('id'));

Happy Coding!

Touheed Khan
  • 2,149
  • 16
  • 26
  • In addition to this, I wanted to ask. Since my Sell Stock does not actually do the function, but it pops up a modal of paramters for user choice. So there is another button 'Place Sell Order' that will do the myFunction. Is there a way to carry that 'Sell Stock' button as (this) to the modal? – Bao Thai Apr 15 '17 at 07:32
  • Maybe something like object callback..? – Bao Thai Apr 15 '17 at 07:50
  • are you using bootstrap modal ? if yes, which version of bootstrap you are using ? – Touheed Khan Apr 15 '17 at 08:24
  • v3.3.7 from bootstrap website itself – Bao Thai Apr 15 '17 at 18:20
  • I am having trouble to just get the modal to trigger with yours, as well as using [here](http://stackoverflow.com/questions/26187370/bootstrap-modal-relatedtarget-is-undefined). Please look at my edit. – Bao Thai Apr 17 '17 at 04:43
  • Excuse my spacing, it did not transfer as well as expected. I tried turning it into a class and do `$(".sellButton").on('click', function(e) { alert('test 1'); });` but was not successful either – Bao Thai Apr 17 '17 at 05:25
  • I think I found the error. It was being add dynamically so I had to use a `document.ready`. Sorry, last question, how should I go access the property of invoker? It just returns an Object var – Bao Thai Apr 17 '17 at 05:52
  • var $invoker = $(e.relatedTarget); use $($invoker) to select invoker element. No need to say sorry buddy, stack overflow is about helping each other – Touheed Khan Apr 17 '17 at 06:16
  • use this to get id, $($invoker).attr('id') – Touheed Khan Apr 17 '17 at 06:32
  • You're the dude. Thank you for all the trouble haha – Bao Thai Apr 17 '17 at 06:34
  • @BaoThai Thanks buddy, I enjoyed solving your issue :) – Touheed Khan Apr 17 '17 at 06:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141857/discussion-between-bao-thai-and-touheed-khan). – Bao Thai Apr 17 '17 at 07:20
0
  1. use class and TD

    "<tr> <td>". $arrayval"</td> <td class='need'>"."(Need)"."</td> <td>".$arrayval."</td> <td>".$arrayval."</td> <td>".$arrayval."</td> <td><button class='sellStock btn btn-primary' data-toggle='modal' data-target='#modal1'>Sell Stock</button></td> </tr>";

  2. use jQuery consistently

    $(".table").on("click",".sellStock",function() { var cellContent = $(this).closest("tr").find(".need").text(); });
    or
    $(".table").on("click",".sellStock",function() { var row = $(this).closest("tr"); // handle row here });

mplungjan
  • 169,008
  • 28
  • 173
  • 236