Passing grails select value in actionSubmit to Javascript method - grails

I have this snippet of code below. I want to pass the value of the select availabilityChoice as a second parameter to my updateAvailability javascript method.
I've tried everything (including an optionKey). How do you do this?
<label>Select Contact Availability:</label><g:select name="availabilityChoice" from="${['Inactive', 'Active']}" value=""/>
<g:actionSubmit type="button" value="Update Availability" onclick="updateAvailability('contactList', availabilityKey.value);"/>
I should add that contactList is a list of check boxes that were selected. So I need to pass that list of selected checkboxes, the value of the select box, and update those selections with the value in the select box.

Set an ID for <select> by using id attribute, and then you can refer to that element in javascript.
<label>Select Contact Availability:</label>
<g:select id="mySelect" name="availabilityChoice" from="${['Inactive', 'Active']}" value=""/>
<g:actionSubmit type="button" value="Update Availability" onclick="updateAvailability('contactList', document.getElementById('mySelect').value);"/>

Related

How can I enter a param for grails-gsp-link Tag in a gsp Page

I have a link via link tag, to a controller where a list function is coded. I want to execute the list function with a selection, e.g. a Year. I would like the user to have the possibility to enter the Year or any other selection criteria on the gsp-page and to route that user input to the params entry in the link tag. Link tag only offers:
<g:link action="list" params="[year: '2018']">myList</g:link>
with 2018 hard coded.
I need it as a user entry.
How can I manage it?
As Mike W suggested, you should use a form for this. (If that is entirely not possible, there are alternatives, but form is the best option.)
To provide a simple example:
<g:form controller="myController">
<label for="year">Year:</label>
<input type="text" id="year" name="year" />
<g:actionSubmit action="list" value="List" />
</g:form>

Get Textbox Value in New Page in Sitecore MVC

I am using Sitecore 7.1. I created one search page and created a textbox by using the Web Form For marketer in it. On button click I want catch the text box value in a new page name search-result. How can I catch the WFFFM text box value in New page? I Don't want use JavaScript click event and query string.
<form class="search-form" action="/searchresults" method='get' >
<label><input type="text" name="searchText" id="searchText" placeholder="#placeholderText"></label>
<input type="submit" name="submit" value="" class="#cssClass">
</form>
But i am getting below type of URL
http://www.xyz/searchresults?searchText=searchTextHere&submit=
Can you capture the "onsubmit" event and update the URL to whatever you need using Javascript? You only said not to use onclick event. I'm assuming that you may want to have /searchresults/ as a result. Of course, I assume that your routes are set for this and searchresults can process this correctly.

Input type='image' as submit button behaves differently than 'submit'

I have multiple buttons submitting a form. I check to see which button was clicked by the input value that gets passed into my controller.
public async Task<ActionResult> MyAction(MyModel model, string command)
The html below works. In my controller command equals myValue
<input type="submit" name="command" value="myValue" />
The html below doesn't work. In my controller command is null
<input type="image" name="command" src="..." value="myValue"/>
W3C says the type 'image' is a submit button
So why are these two input elements behaving differently?
An image submit button indeed behaves differently, by definition, both according to the expired working draft linked to in the question and the current HTML5 CR draft and the HTML 4.01 recommendation. According to HTML5 CR, construction of the form data set does not add a name=value pair for an image submit button but name.x=... and name.y=... with the coordinates of the clicked location as values. This corresponds to old definitions and browser practice, except that Chrome additionally adds a name=value pair.
Thus, in your case, the form data will contain the fields command.x and command.y, with values that you are probably not interested in. This is what you need to take as starting point when coding the form handler. This means that if you need to recognize which of several image buttons was used, you need to give them different name attributes.
You can have one hidden field and set the command name in that hidden field which ever button is click and get the name of the button that submited the form on server side.
Javascript
$(".submit").click(function(){
var commandName = $(this).attr("val");
$("input[type=hidden]").val(commandName);
});
HTML
<form>
<input type="hidden" name="command">
<input type="submit" value="submitbtn" class="submit" />
<input type="image" src="..." value="imagebtn" class="submit"/>
</form>
The simplest (and maybe not safe) way to accomplish this task is with inline JQuery, here's how :
VIEW :
<input type="image" src="..." onclick="$('#hidImgValue').val('Image1');" />
<input type="hidden" id="hidImgValue" name="command" value="none" />
CONTROLLER :
public async Task<ActionResult> MyAction(MyModel model, string command)
Command var will have "Image1" as value.

Retaining multiple selectbox values in struts2

Hi i have a problem with the multiple select box of struts2.
<s:select name="test" id="test" multiple="true" size="2" list="testlist" theme="simple" listKey="testkey" listValue="testvalue" />
The select box name "test" is a field name in my entity.The list data is displaying and i can select multiple items and its stored in the database.But the problem is i could not retain the selected values while modification.But i have noticed that i can retain the value if i select only one item.Any idea?.
you have to define attribute "value", check the example:
<s:select label="Pets"
name="petIds"
list="petDao.pets"
listKey="id"
listValue="name"
multiple="true"
size="3"
required="true"
value="%{petDao.pets.{id}}"
/>

How can I make my own check box in MVC?

I would love to use the:
Html.EditorFor(model => #Data.Test.Correct)
To create a checkbox.
However the source of my data is different from the data that needs to be updated. I know this may sound confusing but I get my data from a LINQ select query and then need to update in a different place.
The only way around this seems for me to hand code the HTML for the checkbox. But can someone give me an example of how I do this. For example, how can I code in the setting of checked=true?
You may write HTML codes like below to create a selected or unselected checkbox
<input type="checkbox" name="option1" value="1" checked="checked" />
<input type="checkbox" name="option2" value="2" />
But you will need to write additional code manually to determine which checkbox should be selected, if your checkbox is dynamic generated or being filled in with stored data.

Resources