views:

105

answers:

7
+2  A: 

Get rid of the ID attributes, add a common class array, and give them all a name of array[].

Then use this in your jQuery;

$('input.array').click(function() {
    var id = $(this).val();      
    $.ajax({
        here goes type, url, data and else
    });     
})  
TheDeadMedic
It doesn't work, it gives a value - the value of input.
hey
Then use `var id = $(this).attr('id').replace(/[^0-9]/, '')` instead!
TheDeadMedic
A: 

I would try add class on each array.

<input type="button" class='array' id="array[1]" value="Value1" />
<input type="button" class='array' id="array[2]" value="Value2" /> 
<input type="button" class='array' id="array[3]" value="Value3" /> 


$('.array').click(function() {
   var id = $(this).attr('id');       
    $.ajax({
        here goes type, url, data and else
    });     
})  

Hope it helps

Jerry
No, this is not what I need, because my class is already defined and I have to do it without using class.
hey
A: 

Use a regexp to extract the number

var reId = /[(\d+)]/;
$('#array').click(function() {
    var id = reId.exec($(this).attr("id"))[0];       
    $.ajax({
        //id is now a string containing only the number
    });     
})  
Sean Kinsey
Doesn't quite work. You need to escape the `[` and `]` and reference index `1` of the result, or don't escape and just put the `+` outside them. :o)
patrick dw
A: 

A crude approach:

$(this).attr("id").replace(/[^0-9]*/ig, "")

Full example:

<!DOCTYPE html>
<html>
<head>
    <title>2939346</title>
    <script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
    <script>google.load("jquery", "1");</script>
    <script type="text/javascript" charset="utf-8">
        $(document).ready(function(){
            $("input[type=button]").click(function(){
                alert($(this).attr("id").replace(/[^0-9]*/ig, ""));
            });
        });
    </script>
</head>
<body>
    <input type="button" id="array[1]" value="Value1" />
    <input type="button" id="array[2]" value="Value2" /> 
    <input type="button" id="array[3]" value="Value3" />
</body>
</html>
The MYYN
+4  A: 

First of all you should not be using [] in id values ... only a-zA-Z0-9-_.:

look at the html ID attribute for more info on the specs..

you should name them something like id_1 and extract the number with regular expressions like the other answers suggest...
(remember that you cannot start the id value with a number only a-zA-Z)

Gaby
A: 

if your storing content in your elements i wuold use data-* to store them, For exmaple instead of using arrays i would use a json string

<input type="button" class='array' data-json="{id:22,defualt_val:'Value1'}" value="Value1" />

then within your javascript code i would use!

$('input[data-json]').click(function(){
    $data = $('[data-json]',this)
    $.ajax({
        data:$data
    });     
}) 

This is a method used to store content to an element such as original-title etc.

RobertPitt
it is generally not a good idea to use arbitrary attribute names (*although yours is HTML 5 compliant, so i am just posting this as a general read for anyone interested*) .. a good discussion can be found at http://stackoverflow.com/questions/994856/so-what-if-custom-html-attributes-arent-valid-xhtml
Gaby
A: 

Hi, I think i can help you solving this problem. As far as I understand input ids start with array keyword and contains original id within bracket which you wanna fetch and send on an ajax call. For elements that start with some predefined string you can use $("[name^=value]") jquery selector.

$("input[id^='array']").click(function() {
    var id = parseInt($(this).attr("id").split("[")[1].slice(0,-1));     
   //above variable should contain whatever you put inside brackets. 
    $.ajax({
        here goes type, url, data and else
    });     
});

That should give desired results. Cheers Ayaz Alavi

Ayaz Alavi