views:

282

answers:

2

Is there a workaround for Internet Explorer to implement the functionality offered by 'this' javascript keyword to get the dom element that triggered the event?

My problem scenario is : I have a variable number of text fields in the html form, like

<input type="text" id="11"/>  
<input type="text" id="12"/>

..

I need to handle the "onchange" event for each text field, and the handling is dependent on the 'id' of the field that triggered the event. So far I understand that my options are: 1) attach a dedicated event handler for each text field. so if I have n fields, i have n different functions, something like:

<input type="text" id="11" onchange="function11();"/>  
<input type="text" id="12" onchange="function12();"/>

but the text fields are added and removed dynamically, so a better way would be to have one generic function instead.

2) use the 'this' keyword like:

<input type="text" id="11" onchange="functionGeneric(this);"/>  
<input type="text" id="12" onchange="functionGeneric(this);"/>

But this option does not work with Internet Explorer.

Can anyone suggest a work around for getting it work in IE or some other solution that can be applied here? Thanks.

+1  A: 

The second option probably does not work because element IDs must start with alphabet or the underscore character (at least, according to the spec).

I would opt for something like this:

// make the ids start with a word, like "foo", followed by "_", followed by a number
$("input[id^='foo_']").change(function() {
    doSomething(this.id.split("_")[1]); // extract the number, pass to function
});

That will attach a change handler to all of your inputs with IDs starting with 'foo', and split the number out of the ID to pass to the generic function which works on the number.

karim79
Beat me to it. Firefox seems to tolerate ID's that start with numbers, but IE sticks to the spec on this one (for once).
Byron Sommardahl
I did a little test, it **do** work in IE. This thus doesn't explain the actual problem he is having.
BalusC
@BalusC - He says "and the handling is dependent on the 'id' of the field that triggered the event." So, he's probably trying to use the ID within his `genericfunction()` which will lead to IE choking.
karim79
Thanks Karim . I didnt know about the ID specification.But BalusC is right. The problem is not with the ID's. I actually have the id's as 'text_11' , and 'text_12'. So the real issue is to get the dom element that triggered the event
Ron
the function: doSomething(this.id.split("_")[1]); will not work in IE due to the "this" keyword.
Ron
@Ron: I can't reproduce it. Please post an http://sscce.org as I asked in a comment on your question. @Karim: again, I did a test and it works in IE6/7/8.
BalusC
@BalusC - I believe you :), @Ron did you put the event handler in a `$(document).ready(function() {...});` block? It might be helpful to past your function also.
karim79
+2  A: 

I can't reproduce your problem. Here's an SSCCE based on the latest information in comments:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2618458</title>
        <script>
            function functionGeneric(id) {
                alert(id); // Shows either 11 or 12 correctly.
            }
        </script>
    </head>
    <body>
        <input type="text" id="text_11" onchange="functionGeneric(this.id.split('_')[1]);"/>  
        <input type="text" id="text_12" onchange="functionGeneric(this.id.split('_')[1]);"/>
    </body>
</html>

It works fine in all major browsers I have here. Your actual problem lies somewhere else. Until you come up with more detail, or better, an SSCCE, it's shooting in the dark to the root cause.

BalusC
Thanks BalusC. it works.
Ron