user3368504 user3368504 - 5 months ago 19
Javascript Question

Passing returned jquery var to another jquery

What is the best way to get the following returned var (in jQuery):

jQuery(document).ready(function() {
var theFormID = jQuery(parent.document).find('.theformclass').find('input[name="form_id"]').val();
});


the above code is ok as it returnes the value i need - so the question is how do i "paste" (print) it in the script that comes after it (where it says:
HERE-IS-WHERE-I-NEED-THE-RETURNED-VALUE)


jQuery(parent.document).find('#abc_HERE-IS-WHERE-I-NEED-THE-RETURNED-VALUE_number_103').fadeToggle("slow", "linear");


thanks for your help :)

https://jsfiddle.net/jhk926ya/

EDIT: thanks to @brian and @showdev - both solutions were correct, problem on my side was jquery sizzle.

works perfectly :) thank you again guys

Answer

EDIT: I cleaned up my answer a little bit, should be a bit more readable. You just need to break up your string and concatenate it with the returned value.

jQuery(document).ready(function() {
    var $parent = jQuery(parent.document); // cache this so jquery doesnt have to instantiate the same object twice

    var theFormID = $parent
        .find('.theformclass')
        .find('input[name="form_id"]').val();

    var $form = $parent
        .find("#abc_" + theFormId + "_number_103")
        .fadeToggle("slow", "linear");
});