Becky
Becky

Reputation: 123

Retrieving value of elements on page load for use in JQuery functions

I'm trying to write a function that will update the URL slug field of a form when the first and last name fields are updated. But only if the slug is empty on page load.

I have successfully gotten the slug to update when any changes are made to the first/last name fields. I just can't quite figure out how to limit the updates to cases where the value of the slug field was empty to start with.

A problem I'm having is the form is loaded via AJAX so if I test for the length of the slug field on page load, it will always come back empty.

Any ideas on how I could edit this?

var FirstName = jQuery('input#FirstName').attr('value').replace(/[^\w]/gi, '');
var LastName = jQuery('#LastName').attr('value').replace(/[^\w]/gi, '');
jQuery('#URLName').attr('value', FirstName + "_" + LastName);

Upvotes: 1

Views: 107

Answers (1)

jfriend00
jfriend00

Reputation: 707696

If the form is loaded via an AJAX call and you need to test a value in that form, then you need to hook into the AJAX code that loads the form and call your code from the completion of the AJAX call.

For us to advise you on how to hook into the AJAX call, we would need to see the AJAX code.

If you can't hook into the AJAX call and you're willing to implement a bit of a hack, then you could start polling the page upon page load with setInterval() and continue until the elements of the form are in the page and then stop the interval and check the form then.

Upvotes: 2

Related Questions