0

Here's my code:

$("#addhash").submit(function(event) {
        // Cancel the default action
        event.preventDefault();

        // Store the value and remove any white space or disallowed chars
        var hash = $("#hashtoadd").val().replace(/\s/g, "").replace(/\W/g, "");

        // Check if anything was entered
        if(hash) {
            // Add the hashtag to the array
            hashes.push(hash);
            localStorage.hashes = JSON.stringify(hashes);
            var key = hashes.length - 1;

            // Clear the form field
            $("#hashtoadd").val("");

            // Set others to inactive
            $("#hashes").children().removeClass("active");

            // Add it to the list and make it the active page
            $("#hashes").append("<li class='active' id='" + key + "'><a href='#' class='close'>&times;</a><a class='hashtag' href='#" + hash + "'>#" + hash + "</a></li>");

            // Load the tweets
            getTweets(hash);
        }
});

This code will only work in Chrome. It appears as though event.preventDefault(); isn't even firing. I tried to put an alert dialog at the very beginning, but that's not showing either which leads me to believe it never even gets in here. I've tried using just a click () event and also using just a submit() event, but those don't work either.

EDIT: I think the problem is that the event handler is never being called in the other browsers. I've updated the code to reflect how it currently is. Still not working though.

EDIT2: As requested, here is the form itself:

<form class="navbar-form pull-right" id="addhash">
    <div class="input-prepend input-append">
        <span class="add-on">#</span>
        <input type="text" placeholder="tag" tabindex="1" id="hashtoadd">
        <button class="btn" type="submit" href="#" id="submit"><i class="icon-plus"></i></button>
    </div>
</form>
18
  • 1
    Can you verify that the handler is called in other browsers?
    – Halcyon
    Jan 4, 2013 at 17:06
  • Put alert("yep"); at the top of the function.
    – Halcyon
    Jan 4, 2013 at 17:10
  • Oh okay that's what I was trying to explain. So I guess the answer is no, the handler never gets called. Jan 4, 2013 at 17:14
  • Is #addhash a <form> element?
    – Derek
    Jan 4, 2013 at 17:15
  • 1
    Are you using the latest version if jQuery 1.8.3? Did you actually include jQuery? Do you have an example to look at since this could be many things? One possibility would be if your dynamically creating the submit button you will need to attach the submit handler to something like the document. Also .live() as of jQuery 1.7 has been deprecated and you should be using .on() instead. Jan 4, 2013 at 17:16

1 Answer 1

0

Not sure if this is exactly your issue but

"As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers."

.live only needs to be used when the object/s selected don't exist in the DOM for all other cases you can use .bind() or in the case of your form .submit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.