1

I am trying to make links work which we click to change the language of website using JavaScript. Here is the HTML code:

'<form action="" method="post" name="currlang">'.
    '<input type="hidden" name="languageval" value="'.$session->value("language").'"/>'.
    '<a onclick="langa(this, \'en\');" class="langs'.($session->value("language")=='en'?" active":"").'" id="en"></a>'.
    '<a onclick="langa(this, \'it\');" class="langs'.($session->value("language")=='it'?" active":"").'" id="it"></a>'.
    '<a onclick="langa(this, \'pl\');" class="langs'.($session->value("language")=='pl'?" active":"").'" id="pl"></a>'.
'</form>'.

and here the javascript:

function langa(obj, valu) {
    document.getElementsByName("languageval")[1].value=valu;
    document.getElementsByName('currlang')[1].submit();
}

The script works fine on new browsers (Firefoxe, Opera, Chrome, IE9) but when testing it on IE8 it gives an error:

"'document.getElementsByName(...).1' is null or not an object".

After some research I found out that getElementsByName() is not supported by IE8 and above and maybe the solution would be to use jQuery and this is where I need help. How can I make this work with jQuery or without it?

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
PLM
  • 35
  • 9

2 Answers2

1

You can try using an ID or class to achieve the same purpose instead of name.

Example:

<div id="foo">This is id of foo</div>
<div class="choo">This is class of choo</div>

Then if you use ID then use:

document.getElementById('foo');

and with class use:

document.getElementsByClassName('choo');

I hope this helps!

pathurs
  • 643
  • 1
  • 5
  • 20
  • Thank you for your help pathurs but it does the than mikeswright49 it just refreshes the page. – PLM Oct 25 '12 at 13:43
  • Can you post more code? It's hard to understand it when it's only a very small section – pathurs Oct 25 '12 at 20:42
-1

For this to work the way you want switch name to Id.

<script type="text/javascript" language="javascript">
   function langa (obj, valu){
      $("#languageval").val(valu);
      $("#currlang").submit();
   }
</script>

The # will pull the element with that Id and val() is the get/set for the element's value.

Edit: added the type and language tags to

mikeswright49
  • 3,381
  • 19
  • 22