synergy989
synergy989

Reputation: 119

Grabbing ID by Prefix

I have a question regarding grabbing content from an e-mail form before/while it submits.

I am using the code

        <script>
            $('#nm_mc_form_id-1352905826').submit(function(e){
                // Grab the email address from the form
                var email = $('#EMAIL-1352905826').val();
            });
        </script>

As you can see the from id and the email id are dynamic. Is there any way to grab the ID's based on their prefix?

I did see in another SO (yes I have looked around ;)) something along the lines of:

$('#id').find('p')

Just not sure how that works or if it's what I need?

I am one of those guys who likes to learn so please provide a little insight as to what your recommendation is.

Cheers!

Upvotes: 0

Views: 80

Answers (3)

Sushanth --
Sushanth --

Reputation: 55750

You can use attribute starts with ^ or attribute contains selector *

$('[id^="nm_mc_form_id-"]')  // Starts with

$('[id*="nm_mc_form_id-"]')  // contains

Upvotes: 1

Jay Blanchard
Jay Blanchard

Reputation: 34426

You could use an attribute selector -

var email = $('[id^="EMAIL-"]').val();

Upvotes: 1

Adil
Adil

Reputation: 148150

Use wild card ^ is used for element starting with given string.

$('[id^=nm_mc_form_id-]').submit(function(e){
            // Grab the email address from the form
            var email = $(this).val();
});

Upvotes: 1

Related Questions