1

I found some fantastic code that meets the needs for my project from here:

http://www.askthecssguy.com/examples/rowlock/example9.html

I've modified the code so that when the user selects an item and then clicks a button the data is processed (e.g. erase the data from a database on the server)

I have not been able to figure out is how to disable the action from occurring again. I've tried disabling the radio button. The code somehow reactivates the radio button. I've set the radio button to null. That too didn't work.

My last thought was to change the text in the 2nd column (the first is the radio button) as a means of testing if the code has already been deleted but I simply can't figure out how to access that column and modify the text.

This is the original code from the link above:

function lockRow() {
var tables = document.getElementsByTagName("table");
for (var m=0; m<tables.length; m++) {
    if (tables[m].className == "pickme") {
        var tbodies = tables[m].getElementsByTagName("tbody");
        for (var j=0; j<tbodies.length; j++) {
            var rows = tbodies[j].getElementsByTagName("tr");
            for (var i=0; i<rows.length; i++) {
                rows[i].oldClassName = rows[i].className;
                rows[i].onclick = function() {
                    if (this.className.indexOf("selected") != -1) {
                        this.className = this.oldClassName;
                    } else {
                        removeSelectedStateFromOtherRows();
                        addClass(this,"selected");
                    }
                    selectRowRadio(this);
                }
            }
        }
    }
}

Which I hacked down to:

function testloop() 
{
var tables = document.getElementsByTagName("table");
    for (var m=0; m<tables.length; m++)     {       
    var tbodies = tables[m].getElementsByTagName("tbody");
    for (var j=0; j<tbodies.length; j++) {
        var rows = tbodies[j].getElementsByTagName("td");
        for (var i=0; i<rows.length; i++) {
          r = rows[ i ] ;
                 // r is the radio button. 
                 //How do I access the text in the 2nd column?
        }           
    }
}

}

If the row with 'Domestic' was selected, I'd like to change 'Domestic' to 'Deleted'. I can detect that in other code and handle that action.

Does this make any sense? Is there a smarter or easier way of what I'm trying to accomplish? My Javascript abilities are sorely lacking. Thank you.

<tr>
<td>
    <input
        type="radio"
        name="choice"
        value="walalala" />
</td>
<td>Domestic</td>
<td>Titanic</td>
<td>$600,788,188</td>

1 Answer 1

2

You have several choices depending on what you really want to do.

  1. You can simply remove the corresponding row - user can't select it and other code won't find it.
  2. You can add for example "removed" class to the row/input when user sends it to your server. Next time user wants to send anything, check if currently selected row doesn't have "removed" class.
  3. Same as 2 but using HTML5 data-xxx attributes.

Something like this should work (using jQuery):

$('button').click(function(event) {
  var i = $('input:checked');
  if (i.hadClass('removed'))
    event.preventDefault();
  else
    i.addClass('removed');
});
Sign up to request clarification or add additional context in comments.

11 Comments

I don't know how to remove the row. That would, of course, be the ultimate solution. How would I go about doing that? The button is completely unrelated to the code that I snatched so I don't know who to tie the button back to the loop. I don't know jQuery at all. I'm just getting back into Javascript. Even if my initial solution of getting the text column is not the best, how would I go about obtaining it within that loop that I've shown? I'm curious. Sorry for being fairly clueless but everyone starts at the ground floor. Thank you.
JavaScript has a parent.removeElement(element) function, so in your loop you could get var rows = tbodies[j].getElementsByTagName("tr"); tbodies[j].removeElement(rows[x]); with x being the index of row that you want to remove. If you don't know jQuery, you should pick it up. It would make your life easier with $('input:checked').parent().parent().remove(). Dojo or Prototype or one of many other JS libraries woud work too.
My bad, it's removeChild, not removeElement, sorry.
Yeah, it does give you the td tag - you need child.textContent to actually get the text, like rows[x].children[0].textContent
That one is easy=) children[1].textContent = "new text"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.