1

Is there any reason why I'm not able to see object values from JavaScript using Firefox, but IE and Chrome see them without problem?

For example:

<div>
<input type="text" id="clientID" />
<input type="submit" id="search" value="Submit" class="submitButton" />
</div>

JavaScript:

<script type="text/javascript">
$(document).ready(function () {
        $("#searchDisputes").click(function () {
              if(clientID.value.toString() != "") {
                    //do something
              }
        }
}
</script>

Firefox tells me that clientID does not exist, however IE and Chrome work just fine.

I am able to access it using jQuery $("#clientID"), but before changing a good bit of code around, I would like to understand why this doesn't work in Firefox, but works ok in other browsers.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
Brosto
  • 4,445
  • 2
  • 34
  • 51
  • 3
    you aren't. $("clientID") is searching for html tag, not an ID – genesis Sep 21 '11 at 15:34
  • Should be `if ($('#clientID).val() ...` instead. `id` fields don't get put into the variable namespace, only `name` attributes do. – Marc B Sep 21 '11 at 15:36
  • 1
    Sorry, I omitted the "#". Fixed. – Brosto Sep 21 '11 at 15:36
  • My question is not really about how to access the variable using JQuery (I know how to do that, I just had a typo in my initial post). My question is really about why I can access something just using it's name in IE and Chrome, but not in FireFox. Is IE and Chrome just allowing for non-standard access to the variables? – Brosto Sep 21 '11 at 15:39

1 Answers1

2

You are assuming that giving an element an id will create a global variable with the same name as the id containing a reference to the element. There is no reason browsers should do this.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335