views:

178

answers:

4

In my JavaScript code, I have to make a line like this (I use smarty template engine, that is the literal stuff).

<script language="javascript" type="text/javascript">
    function ajaxTextKill() {
        // ...etc.
        ajaxRequest.open("GET", "functions.php?action=kill{/literal}&id="+IWANTMYIDHERE+"&p={$smarty.get.page}&c={$smarty.get.sel}{literal}", true);
        ajaxRequest.send(null);
    }

After this in my HTML code,

<input type="button" id="87" value="del" onClick="return ajaxTextKill();" />

I'd like to give the JavaScript the input's id value. How to do this?

+1  A: 

The HTML

<input type="button" id="87" value="del" onClick="return ajaxTextKill(this.id);" />

The JavaScript

function ajaxTextKill(id){
...etc.
ajaxRequest.open("GET", "functions.php?action=kill{/literal}&id="+id+"&p={$smarty.get.page}&c={$smarty.get.sel}{literal}", true);
ajaxRequest.send(null); }
TiuTalk
You're brainwashed. JavaScript != jQuery
BalusC
@BalusC - It was corrected ;)
TiuTalk
+1  A: 
<input type="button" id="a87" value="del" onClick="return ajaxTextKill(this.id);" />
Harmen
A: 

Let the element pass itself to the function by ajaxTextKill(this). Then just grab its ID by element.id.

function ajaxTextKill(element) {
    var buttonid = element.id;
    ajaxRequest.open("GET", "functions.php?action=kill{/literal}&id="+ buttonid +"&p={$smarty.get.page}&c={$smarty.get.sel}{literal}", true);
    ajaxRequest.send(null);
}
BalusC
The `this` will work inside the function?
TiuTalk
Indeed not in this construct. Fixed :)
BalusC
No, it won't, because of the method used to handle the event.
Andy E
+3  A: 

You don't necessarily need the ID if you pass a reference to the field itself.

<input type="button" id="87" value="del" onClick="return ajaxTextKill(this);" />

And access the ID like so:

function ajaxTextKill(object){
    alert(object.id);
}
Dolph