ksa
ksa

Reputation: 331

how to get exact id values of a table

I'm trying to sort out this problem but have yet to get results. I tried to get the exact id values of the row which I edit in order to perform an update.

I can get get my current cell value but I can't get the corresponding id values of the cell. I iterate over the table id value which in turn get's all the id values of the table.

I'm using this jquery code for iteration:

$('#table').ready(function() { 
     $(this).find('.filename').each( function() { 
          alert($(this).html()); 
     }) 
})

Then I tried this simple JavaScript:

var fid= document.getElementById("filename").textContent;

It gets only the first id value of the table.
For example, if I edit the first row the id value is 53.
If I edit the second row the id value should be 52 but it gets only 53.

See this link, I have posted this question previously.

Upvotes: 0

Views: 368

Answers (3)

ksa
ksa

Reputation: 331

hi thanks for ur response ,i tried a different script and it was working as i thought here is the code

function doubleclick(table,id1)
        {

            table.innerHTML="<input type=text name=tab onBlur=\"javascript:submitNewName(this,'"+id1+ "');\" value=\""+table.innerHTML+"\">";
            table.firstChild.focus();
           // alert(id1);
        }
        function submitNewName(text,id2)
        {
            text.parentNode.innerHTML=text.value;
            $.ajax({
        url: 'update',
         data:{text1:text.value,fileid1:id2},
        type:"POST",
        dataType:"json",
        error:function(data){
           //alert('error')
        },
        success: function(data) {
            //alert('updated!')
        }
    });

        }

html: //when double clicked it will pass both ${item.filedesc},${item.id}

<td ondblclick="javascript:doubleclick(this,${item.id});" >${item.filedesc}</td>

Upvotes: 0

hjuster
hjuster

Reputation: 4070

I am not quite sure that i understand your question but this will give you ids of all cells within a table:

  $("#table > td").each(function(){
      alert($(this).attr("id"));
  });

Upvotes: 0

PeeHaa
PeeHaa

Reputation: 72682

getElementById gets the element and not the id. The id is what you are providing as parameter.

You could simply do:

$('#table').ready(function() { 
     $(this).find('.filename').each( function() { 
          alert(this.id); 
     }) ;
});

Instead of cluttering the DOM with id's (which should always be unique) you could always use the data-attributes of elements:

<tr data-identifier="53"><td></td></tr>

$('#table').ready(function() { 
     $(this).find('.filename').each( function() { 
          // if this is the row
          alert($(this).data('identifier')); 
     }) ;
});

Upvotes: 1

Related Questions