Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:08:35+00:00 2026-05-30T06:08:35+00:00

This jQuery selector matches a Rails 3 HTML form for a new model: $(‘form[id^=new_]’)

  • 0

This jQuery selector matches a Rails 3 HTML form for a new model: $(‘form[id^=”new_”]’)

I’d like to have a simple focus function run each time a matching form loads. Sometimes the forms are loaded via a simple GET but also via Ajax. In the latter case, the content returned can be either HTML or escaped JS.

I was hoping jQuery would be able to match all cases via the selector, .on(), and the “load” event, but I can’t seem to make that work for ANY case. Code:

$(document).ready(function() {
    $('form[id^="new_"]').on("load", function(){
        console.log("Matched!")
    });
})

Any ideas?


Thanks Justice. I’m afraid I wasn’t able to get your code to work. I’m using the following callback with the new custom event defined outside it as shown and I don’t think the $(‘form’) is triggering the event.

$('.shows-children').bind('ajax:success', function(evnt, data, status, xhr){
    var boxSelector = '#' + $(this).data("shows");
    $(boxSelector).html(xhr.responseText);
    $('form').trigger('customevent');
});

$(document).on('customevent','form[id^="new_"]', function(){
    console.log('Matched!')
});

(I’m surprised it seems more involved than expected to have jQuery act on HTML returned in an Ajax response.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:08:37+00:00Added an answer on May 30, 2026 at 6:08 am
    $(document).on("change","form[id^=\"new_\"]" function(){
            console.log("Matched!")
        });
    

    For delegation, you want to delegate the original selector to a parent, as the event will bubble up.

    However, load does NOT bubble up. In this case, change may suffice, but it’ll trigger and attempt to see if the delegate is valid every time the document changes.

    I would then suggest that you create a custom event after AJAX loads for the form.

    Example:

    $(document).on("customevent","form[id^="new_"]" function(){
        console.log("Matched!")
        $.ajax(url, function(response){
            //success
           $(document).append(response);
           $('form').trigger('customevent');
        });
    });
    

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jQuery code $(#selector).html('<a href=url>text</a>'); where url and text are JavaScript variables.
Does anyone have any idea what this jQuery selector will do? object.find('td:eq(1) div div');
Given this HTML: <div class=OpenIDSelector>some text</div> Why does this JQuery selector match it on
I have this jquery selector $('.accordion label').live('click',function() { alert(hello clicky clicky); } but if
if you got something like this: jQuery(selector).focus(function(){...}); jQuery(selector).focus(function(){...}); the focus will trigger twice, is
I have a form which is built dynamically using this jQuery plugin http://code.google.com/p/jquery-dynamic-form/ When
I would like to know if this is possible, with a jquery selector, to
I have a jquery selector that looks like: var val = John's cars; $('#subject
Is there a clever jQuery selector for selecting a text node like this: <div><input
I have this jQuery code that queries an API on a keyup event (via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.