2

I got most of this code from an answer to This question

<script type="text/JavaScript">
  function newInput(NIFormName,NIname,NIvalue,NItype='hidden'){
    var theForm = document.forms[NIFormName];
    var input = document.createElement('input');
    input.type = NItype;
    input.name = NIname;
    input.value = NIvalue;
    theForm.appendChild(input);
    return true;
  }
</script>

That seemed to work in FF but not IE 11.

So, I did a search and it looked like I needed a different method and I tried:

<script type="text/JavaScript">
  function newInput(NIFormName,NIname,NIvalue,NItype='hidden'){
    var theForm = document.forms[NIFormName];
    var input = document.createElement('input');
    input.setAttribute('type', NItype);
    input.setAttribute('name', NIname);
    input.setAttribute('value', NIvalue);
    theForm.appendChild(input);
    return true;
  }
</script>

It still works in FF 26 and does not work in IE.

When I say "it does not work" I mean I see no errors and the field is not added to the submitted data.

This is the top of my form:

<form name="writenew" method="get" action="formtest.php" onSubmit="newInput('writenew','first_added_field','Yep! I was added.');"> 

and this is formtest.php

<?php
  var_export($_GET);
?>

When I submit it with FF, this is included in the display

'first_added_field' => 'Yep! I was added.',

But in IE it is not.

How do I make this work in IE?

Community
  • 1
  • 1
TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • do you now what errors your getting, if you press F12 in IE then you will get the console up then this should hopefully give you a readable error. – Simon Davies Jan 13 '14 at 21:09
  • looks like your trying to add it on submit, but if the browser is too quick etc it might not be adding it in time to be past to the other page? Why are you adding it on submit. – Simon Davies Jan 13 '14 at 21:12
  • 2
    `method=get"` You're missing a double-quote. – MattDiamant Jan 13 '14 at 21:15
  • @SimonDavies I was adding in with an onclick event before and it didn't work there either. I'll see if I can find an error in my IE console. – TecBrat Jan 13 '14 at 21:15
  • 1
    check and fix what @MattDiamant mentioned as other browsers can work things out but IE tend to just stop / error rather than guess. – Simon Davies Jan 13 '14 at 21:18
  • Okay, I fixed the double quote and edited my question above with that small update. I then removed my onSubmit and added this: `` When I click on the button in FF, the new field appears. In IE it does not. The IE console tells me "'newInput' is undefined" – TecBrat Jan 13 '14 at 21:27

1 Answers1

2

Javascript doesn't support parameter defaults like NItype='hidden'. It's a firefox only feature. See here. Try to use just NItype then check it for undefined inside the function. Here is an example.

Community
  • 1
  • 1
zord
  • 4,538
  • 2
  • 25
  • 30
  • Thanks. I'll try this first thing tomorrow. – TecBrat Jan 14 '14 at 00:53
  • Thanks. I ended up using `function newInput(NIFormName,NIname,NIvalue,NItype){ NItype = NItype || 'hidden'; // since a "falsey" value is not allowed, this creates a default value.` – TecBrat Jan 14 '14 at 13:43
  • Your welcome. Yeah, if the parameter cannot be falsey, then it's a perfectly fine solution. – zord Jan 14 '14 at 14:13