0

How can you run a getelementbyid on an Array and a String in Javascript and set it as a variable that is not null for example foo["dog"] x = getelementbyid(foo[0]+"food") and now x = dogfood

<script>
  var myrows = new Array();   

  $(function() {
    $("#check").click(function(){
      myrows=[]
      $(".head input:checked").not("#selectall").each(function(){
        myrows.push($(this).parent().attr("id"));
      }).value;

      alert(myrows);
    });   

    $("#subbut").click(function(){
      var x;
      var r=confirm("Are you sure you?");

      if (r==true){
        x="You pressed OK!";
      }else{
        Object.cancel;
      }

      **alert( myrows[0]+"servername" + " before" );

      for(var i =0; i< myrows.length; i++){
        alert(myrows[i] +"rootname" +" in loop" );
        var j= document.getElementById(xmyrows[i] +"rootname" );
        alert(j+" after call" );
        var y = document.getElementById(myrows[i]+"servername");

        document.getElementById("id_rootname").value= j.textContent;
        document.getElementById("id_servername").value= y.textContent ;**

        $.post($("#forms").attr("action"), $("#forms").serialize(), function(data) {

        });
      }
    });
  });

</script>
7
  • you would need an iframe target on the form in order to avoid a refresh Commented Jul 17, 2013 at 19:03
  • 2
    You can only submit one form unless you use ajax to pass the form data to the server Commented Jul 17, 2013 at 19:09
  • I am working in django there has to be a way to submit multiple forms on 1 button click Commented Jul 17, 2013 at 19:15
  • 2
    you can submit multiple forms on one click.. solution is Ajax... Commented Jul 17, 2013 at 19:18
  • Is ajax my only solution? I have been running from adding another thing to my many django libraries. Commented Jul 17, 2013 at 19:33

2 Answers 2

1

I don't really understand what the array/string problem is, but from the comments it seems you're looking for a way to do dynamic form submission: Dan Davis has provided the nuts and bolts of the solution in his comment — for each form you need to submit dynamically (without a refresh), create an iframe, then set the respective form's target attribute to that iframe's ID:

<form id="form1" target="#form1Response">
    ...
</form>
<form id="form2" target="#form2Response">
    ...
</form>
<iframe id="#form1Response"></iframe>
<iframe id="#form2Response"></iframe>

You will then need to attach your server response callbacks to the various iframes' load events. Beware though: even an empty iframe fires a load event, so you will need to filter false positives (empty iframe contents) in your callback.

Another caveat: if your server responds with JSON, IE will prompt the user to save the response to the filesystem before your script can intercept — so make sure the MIME type heading is set to text/plain or text/html to make sure the response is loaded into the iframe's DOM.

Sign up to request clarification or add additional context in comments.

1 Comment

I already figured out how to dynamic form submissions the issue is i need to fill in text boxes with data and var j= document.getElementById(myrows[i].valueOf() +"rootname" ); is returning that my j is null when it should be row#rootname.
0

This can happen because java script allows white spaces sometimes if a string is concatenated with a number. try removing the spaces and create a string and then pass it into getElementById.

example:

var str = myrows[i]+"rootname";

str = str.replace(/^\s+|\s+$/g,"");

var str1 = myrows[i]+"servername";

str1 = str1.replace(/^\s+|\s+$/g,"");
var j= document.getElementById(str);
var y = document.getElementById(str1);

document.getElementById("id_rootname").value= j.textContent;

document.getElementById("id_servername").value= y.textContent ;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.