-2

I saw many examples and did the same but I cannot set value with JavaScript.

There is my code

<h:form id="listPlacesForm">
  <h:inputHidden id="hiddenMapCenter" value="#{userPlaces.center}" />
</h:form>

and JavaScript

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(findPosition);
} else
    error('Geo Location is not supported');

function findPosition(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    alert('Start')
    var str = lat + ", " + lng;
    var elem = document.getElementById('listPlacesForm:hiddenMapCenter')
    alert(str)
    elem.value = 'asfsf'
    alert('It works!' + str);
}

All these code works except line: elem.value = 'asfsf'
I tried: elem.value = 'sfd', elem.value = "asfd", elem.value = str

Any ideas?

UPDATE 1
Generated HTML

<form id="mainTabId:listPlacesForm" 
      name="mainTabId:listPlacesForm" method="post"
      action="/DarkComm/app/settings?execution=e3s1" enctype="application/x-www-form-urlencoded">
      <input type="hidden" name="mainTabId:listPlacesForm" value="mainTabId:listPlacesForm" />
      <input id="mainTabId:listPlacesForm:hiddenMapCenter"
             type="hidden"          
             name="mainTabId:listPlacesForm:hiddenMapCenter" value="" />
    <input type="hidden"   name="javax.faces.ViewState"
           id="javax.faces.ViewState" value="e3s1" />
</form>
Tiny
  • 27,221
  • 105
  • 339
  • 599
Bohdan Zv
  • 442
  • 2
  • 5
  • 22

1 Answers1

-2

Change this:

      var elem = document.getElementById('listPlacesForm:hiddenMapCenter')

To this:

      var elem = document.getElementById('hiddenMapCenter')
alexeiTruhin
  • 425
  • 3
  • 9
  • 1
    JSF prepends `id`s with respective parent components unless `prependId` associated with `` is set to false. – Tiny Apr 18 '15 at 21:42
  • Try here: http://jsfiddle.net/AlexeiTruhin/2sops8br/ Result: http://i.imgur.com/Oac9CSl.png – alexeiTruhin Apr 18 '15 at 21:42