Typeerror: Undefined form element only in FireFox

For some reason, I only get the error message in FireFox:

Typeerror: document.forms.myCity.optionname undefined

The script works in all other browsers:

function WriteCookie()
{
    document.cookie = "city" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    cookievalue = document.forms['myCity'].optionname.value + ";";

    document.cookie='city='+cookievalue +'; expires=Fri, 3 Aug 2021 20:47:11 UTC; path=/';
    window.location.href = "http://mywebsite.com";

}

This script is in the header and runs in this form:

<form name="myCity" action="http://mywebsite.com/"  method="POST">

<?php

  function get_terms_dropdown($taxonomies, $args){

    $myterms = get_terms($taxonomies, $args);

    $optionname = "optionname";

    $emptyvalue = "";

    $output ="<select name='". $optionname ."'><option selected='". $selected . "' value='" . $emptyvalue . "'>Select a City</option>'";

    foreach($myterms as $term){

      $term_taxonomy=$term->pa_city; //CHANGE ME

      $term_slug=$term->slug;

      $term_name =$term->name;

      $link = $term_slug;

      $output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";

    }

    $output .="</select>";

    return $output;

  }

  $taxonomies = array('pa_city'); 

  $args = array('order'=>'ASC','hide_empty'=>true);

  echo get_terms_dropdown($taxonomies, $args);

?>

<input type="submit" value="click" name="submit" onclick="WriteCookie()">

</form> 

The error is only in FireFox, any ideas?

+4
source share
1 answer

Your mistake:

Typeerror: document.forms.myCity.optionname undefined

I believe the problem is with this element:

<form name="myCity" action="http://mywebsite.com/"  method="POST">

, id name. , , id, name <form>. -, , , MSN XHTML:

name XHTML 1.1 .

โ€‹โ€‹ XHML 1.1, :

W3 , name HTML 4.0, ID XHTML 1.1.

W3, ; :

name = cdata [CI] , . . . id .

id , :

<form name="myCity" id="myCity" action="http://mywebsite.com/"  method="POST">

name, id, XHTML 1.1.

- , JavaScript id:

function WriteCookie()
{
    document.cookie = "city" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    cookievalue = document.getElementById("myCity").optionname.value + ";";

    document.cookie='city='+cookievalue +'; expires=Fri, 3 Aug 2021 20:47:11 UTC; path=/';
    window.location.href = "http://mywebsite.com";

}

, :

cookievalue = document.forms['myCity'].optionname.value + ";";

:

cookievalue = document.getElementById("myCity").optionname.value + ";";
+4

Source: https://habr.com/ru/post/1542725/


All Articles