views:

231

answers:

2

I'm having a problem with a simple form where i'm attempting to fire an ajax request when someone types something. After debugging I'm left with an alert when the event is fired but it shows up twice, irrespective of the browser. (Ignore the 'rel' attribute in the input tags, that's for something else, unless you think its the rel attribute causing the issue).

<form class="formBig" name="formRegister" id="formRegister" method="post" action="">
    <label class="label" id="locationLabel" for="location">Location</label>
    <input class="input" name="location" id="location" type="text" rel="locationLabel" value="" />
</form>

<script type="text/javascript">
$("#location").click(function() {
    alert($(this).attr('id'));
});
</script>
+2  A: 

You are binding a click event handler to a text input field meaning that it will be invoked when you click and not when you type. Try using the keyup event.

Darin Dimitrov
Good catch there! +1
o.k.w
sorry - im curious - how does this explain click being fired twice?
Scott Evernden
First time you click on the input in order to type the alert shows, then I don't know what the user is doing but probably closing the alert and clicking in the input again to type and second alert shows, etc...
Darin Dimitrov
Because of the label attaching to the input, it will focus on the input when the label is clicked. That will trigger the click too. Though I would think it is a sequence of user actions that caused the double trigger.
o.k.w
@o.k.w, good investigation.
Darin Dimitrov
A: 

I ended up figuring it out, it was because (for some unknown-to-me-reason) the jquery I was including was put in after the form.

However, if I included it within the document.ready, it worked fine. I assume this has something to do with DOM. I dunno, I'm still fairly n00bish when it comes to jquery.

And the document ready is called from within the head-tags via a script include.

Gabriel