24

How can I find the seq number given the id in this example?

<table>
<tr class="row_header thin_border"> 
    </tr><tr id="id33192010101533333"  seq="2">
    <td>20101015</td>
    <td>600</td>
    <td>730</td>        
    <td><a href="#" onclick="deleteActivity(3319,20101015,1);">Click</a></td>
    <td><a href="#" onclick='selectEditActivity("id3319201010153333");'>Click</a></td>
    </tr>

    <tr id="id3319201010151111"  seq="3">
    <td>20101015</td>
    <td>600</td>
    <td>730</td>        
    <td><a href="#" onclick="deleteActivity(3319,20101015,1);"> <img src="/bbhtml/img/deleteAction.png"></a></td>
    <td><a href="#" onclick='selectEditActivity("id3319201010151111");'><img src="/bbhtml/img/editAction.png"></a></td>
    </tr>
<table>


<script>
    function selectEditActivity(pass_id){
        alert("seq# =:" + ???)
    }
</script>
John Slegers
  • 45,213
  • 22
  • 199
  • 169
robert
  • 625
  • 3
  • 12
  • 23

4 Answers4

62

try this

var id = document.getElementById("divId").getAttribute("attributeId");
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
zod
  • 12,092
  • 24
  • 70
  • 106
9

How to get an attribute based on an id

With jQuery :

var seq = $('#id33192010101533333').attr("seq");

Without jQuery :

vvar seq = document.getElementById("id3319201010151111").getAttribute("seq");

You can try both of them at this Fiddle.

Both options should work in any browser!


What you really want

First of all, it's better to name your custom attribute data-seq instead of seq. The HTML5 standard allows custom elements but only considers them valid when their name starts with data-.

Also, it's not a good idea to put your click handlers directly in your CSS. It's better to use the class property or some custom data-action property with a value like edit or delete and then attach an event handler when you finish loading your page. Then, look at the first ancestor that has a data-seq property and get that property.

As one demo says more than a thousand words, here's a demo :

var list = document.querySelectorAll('[data-action="delete"]');
for (var i = 0; i < list.length; ++i) {
    list[i].addEventListener("click", function(e){
        alert('delete ' + e.target.closest('[data-seq]').getAttribute('data-seq'));
    });
}

var list = document.querySelectorAll('[data-action="edit"]');
for (var i = 0; i < list.length; ++i) {
    list[i].addEventListener("click", function(e){
        alert('edit ' + e.target.closest('[data-seq]').getAttribute('data-seq'));
    });
}
table, td {
    border : 1px solid #333;
}

td {
    padding : 10px;
}
<table>
    <tr class="row_header thin_border"></tr>
    <tr id="id33192010101533333" data-seq="2">
        <td>20101015</td>
        <td>600</td>
        <td>730</td>        
        <td><a href="#" data-action="delete">Click</a></td>
        <td><a href="#" data-action='edit'>Click</a></td>
    </tr>
    <tr id="id3319201010151111" data-seq="3">
        <td>20101015</td>
        <td>600</td>
        <td>730</td>        
        <td><a href="#" data-action="delete"> <img src="https://i.stack.imgur.com/mRsBv.png?s=64&g=1"></a></td>
        <td><a href="#" data-action='edit'><img src="https://i.stack.imgur.com/mRsBv.png?s=64&g=1"></a></td>
    </tr>
<table>

Or, if you prefer the jQuery way, you can replace the JavaScript code with the following (much simpler) code :

$('[data-action="delete"]').click(function(e){
    alert('delete ' + $(this).closest('[data-seq]').data('seq'));
});

$('[data-action="edit"]').click(function(e){
    alert('edit ' + $(this).closest('[data-seq]').data('seq'));
});

See also this Fiddle and this Fiddle for the same demo on JSFiddle, respectively without and with jQuery.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
8

Retrieve the DOM element and then get the seq attribute:

document.getElementById(id).getAttribute('seq'); // note: this will return a string, and getElementById might return null in case there is no element with the given id.
Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
  • I am very close.... // this works alert(pass_id) // this works; var A = document.getElementById("id33192010101533333").getAttribute("seq"); alert(A); // but does not when I use the variable var B = document.getElementById(pass_id).getAttribute("seq"); alert(B);You may only edit a comment every 5 seconds.(click on this box to dismiss) – robert Oct 15 '10 at 15:38
1

You want to use objRef.getAttribute('seq') or plan old dot notation objRef.seq

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 2
    Property access like `objRef.seq` won't work for custom attributes since the HTML DOM has no mapping between them. (Except in IE<8 due to bugs.) This is the only time using `getAttribute` in an HTML document is actually the right thing. Although one could argue that custom attributes in general aren't the right thing. – bobince Oct 15 '10 at 15:44
  • @bobince Back it up with a jsbin and I will give you a cookie. I am not sure where you got that info from. – epascarello Oct 16 '10 at 09:11
  • http://jsbin.com/ojiyu4/2 — bof/undefined in all browsers except IE<9 (I lied about IE8! it's 9 where this is finally fixed) due to the aforementioned bug: IE doesn't understand the difference between attributes and properties, causing a wide-variety of well-known problems. In general DOM properties are a different thing to attributes and should not be confused: consider for example the difference between the `value` attribute (input default value) and the `value` property (current value). – bobince Oct 16 '10 at 11:55
  • @bobince did you ever get your cookie? – Kasapo May 10 '12 at 18:22