0

I'm trying to create a simple way to pull information from a custom HTML attribute when I have multiple buttons to click with the same 'id'.

I'd like to know if I can pass the 'url' attribute value from the specific button I press. Right now, it always just grabs the first one...

Code:

<script>
$(document).ready(function() { 
    $(":input[id='alarm']").click(function() {
        alert($("input[url]").attr("url"));
    });
});
</script>

<table id='alarms'>
    <th>ID</th>
    <th>CaseID</th>
    <th>Alarm</th>

    <tr>
        <td width="50">2040</td>
        <td width="180">TEST-111110-123-R4</td>
        <td><input id="alarm" type="button" value="Cancel" url="../silencealarm.php?id=2040"></td>
    </tr>
        <td width="50">2042</td>
        <td width="180">TEST-111110-123-R6</td>
        <td><input id="alarm" type="button" value="Cancel" url="../silencealarm.php?id=2042"></td>
    </tr>
</table>
0

3 Answers 3

2

use this (don't use duplicate id's though)

$(document).ready(function() { 
    $(":input[id='alarm']").click(function() {
        alert($(this).attr("url"));
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! Thank you all. Just one of those dirt simple things that wizz past your head. Can I tag this '#brainfart'? :)
2

While using the custom HTML attribute prefix with data-* and inside your handler this refres to the element itself so just access it with $(this).data("url")

 $(":input[id='alarm']").click(function() {
        alert($(this).data("url"));
    });

Do not use duplicate id, It makes your HTML invalid and your handler will be attached only to the first instance, SO change it to class instead.

Markup

<tr>
        <td width="50">2040</td>
        <td width="180">TEST-111110-123-R4</td>
        <td><input class="alarm" type="button" value="Cancel" data-url="../silencealarm.php?id=2040"></td>
    </tr>
        <td width="50">2042</td>
        <td width="180">TEST-111110-123-R6</td>
        <td><input class="alarm" type="button" value="Cancel" data-url="../silencealarm.php?id=2042"></td>
    </tr>

Script

$(":input.alarm").click(function() {
        alert($(this).data("url"));
});

Demo

Comments

0

just simple change your code to:

$(document).ready(function() { 
    $(":input[id='alarm']").click(function() {
        alert($(this).attr("url")); //or
        alert($(this).prop("url"));
    });
});

Comments

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.