Search code examples
javascriptgrailsgsp

g:javascript working in firefox but not in chrome


this works perfectly as I want it to in Firefox, but it does not work in Chrome. Can anyone point me in the right direction please?

<g:javascript>
    function selectedStatus()
    {
      var index = j("#statusId");
      if(${statusValue} = ${Status.getAllEnums()})
      {
        index.selectedIndex = ${statusValue};
      }
    }
</g:javascript>

I am passing a value for a users status from a controller to a gsp page. I check to see if the value is equal to one of the values in the grails select, and if it is then I set this "current" value as the value appearing in the select box.

Here is my gsp...

<g:formRemote name="custom_status" url="[controller: 'traffic', action: 'status']">
  <h4>
    <g:select id="statusId" name="MyStatus" from="${Status.getAllEnums()}" value="${statusValue}" noSelection="['':'Please Select...']" onload="selectedStatus()" onchange="document.getElementById('sub_status').value = ''"/>
  </h4>
    <g:textField name="sub_status" value="${subStatusValue}" />
    <g:submitButton name="submit_status" value="Apply Status" />
  </g:formRemote>

The select box changes as desired in Firefox but no change occurs in Chrome.

Here is the resulting HTML...

<form onsubmit="jQuery.ajax({type:'POST',data:jQuery(this).serialize(), url:'/portal/traffic/status',success:function(data,textStatus){},error:function(XMLHttpRequest,textStatus,errorThrown){}});return false" method="post" action="/portal/traffic/status" id="custom_status">
  <h4>
    <select name="status" id="statusId" onload="selectedStatus()" onchange="document.getElementById(&#39;sub_status&#39;).value = &#39;&#39;" >

<option value="">Please Select...</option>

<option value="available" >available</option>

<option value="away" >away</option>

<option value="dnd" >dnd</option>

<option value="unavailable" >unavailable</option>

</select>
  </h4>
    <input type="text" name="sub_status" value="In a meeting" id="sub_status" />
    <input type="submit" name="submit_status" value="Apply Status" id="submit_status" />
  </form>
  <br/>

Thanks


Solution

  • It's really strange why value="${statusValue}" doesn't work here. Btw, your Javascript code maybe invalid (i'm not sure what you have as a result JS, but it's very likely), so try following:

    <g:javascript>
        j(document).ready(function ()
        {
          j("#statusId").val('${statusValue}'); //I guess `j` is your prefix for jQuery, right?
        });
    </g:javascript>
    

    and remove onload="selectedStatus()"