1

I'm trying to get the form id and pass it dynamicaly into a script:

jQuery(document).ready(function() {
  var theFormID = jQuery(parent.document).find('.theformclass').find('input[name="form_id"]').val();
});
<script type = "text/javascript" >
  jQuery(document).ready(function() {
    jQuery('.somebutton').click(function(event) {
      jQuery(parent.document).find('#abc_HERE-IS-WHERE-I-NEED-THE-RETURNED-VALUE_number_103').fadeToggle("slow", "linear");
      jQuery('#closeShare').removeClass("hide");
      jQuery('#closeShare').addClass("show");
    });
  });

any help with that? thank you!

rrk
  • 14,861
  • 4
  • 25
  • 41
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Zakaria Acharki Jun 07 '16 at 10:34
  • 1
    you can just pass the id to the html in a hidden field and get that value of hidden field from that other script. – guradio Jun 07 '16 at 10:34
  • You can store the ID in a global variable (window variable) and use it again – Amar Singh Jun 07 '16 at 10:51

2 Answers2

0

you can get your form id dynamic as following..

$(document).on('click', '#id', function(){});

after using this method your script must be like that..

<script type="text/javascript">
    jQuery(document).on("click", ".somebutton", function() {
        jQuery(parent.document).find('#abc_form_HERE-IS-WHERE-I-NEED-THE-RETURNED-VAR-VALUE_number_103').fadeToggle("slow", "linear");
    });
</script>
guradio
  • 15,122
  • 3
  • 30
  • 47
  • thanks for replying :) the var is returned correctly (checked it with "alert"). what i need is to "paste" it dynamicly where you see the "HERE-IS-WHERE-I-NEED-THE-RETURNED-VAR-VALUE" in the code – user3368504 Jun 07 '16 at 10:43
0

You can do a few things here:

  1. Add the value theFormID to the global (window) scope. Use window.theFormID = something. Note that cluttering the global scope is not a good practice.
  2. You can add a hidden element in your DOM where you extract the value from, and read it where you want.

Let us say you have done either of them, and have the value in myValue:

var myValue = $('#hidden-element').val();
var selector = "#abc_form_" + myValue + "_number_103";
$(parent.document).find(selector)...

should work.

EDIT1: After Comment

If you mean to say you need to build your selector on more than one formID, you can maintain an array in the global scope, and listen on it for any changes.

Community
  • 1
  • 1
Wraith
  • 53
  • 5
  • thanks @Wraith i get your idea, but since i have many selectors that i will need to populate the returned var - your suggestion is good for 1 specific selector... no way to "echo" (like in php) an outer jquery var ? – user3368504 Jun 07 '16 at 11:44