How to Select Default Value of a Field

How can I set the default value for an HTML select element?

Set selected="selected" for the option you want to be the default.

<option selected="selected">
3
</option>

setting up a default value of a column in select statement

Yes, you can do this:

SELECT name, city, addr, 12345 AS ph_no
FROM table1

Select default option value from typescript angular 6

You can do this:

<select  class='form-control' 
(change)="ChangingValue($event)" [value]='46'>
<option value='47'>47</option>
<option value='46'>46</option>
<option value='45'>45</option>
</select>

// Note: You can set the value of select only from options tag. In the above example, you cannot set the value of select to anything other than 45, 46, 47.

Here, you can ply with this.

How to SELECT DEFAULT value of a field

"SELECT $group FROM grouptable WHERE $group=DEFAULT( $group ) "

Or I think better:

"SELECT DEFAULT( $group ) FROM grouptable LIMIT 1 "

Update - correction

As @Jeff Caron pointed, the above will only work if there is at least 1 row in grouptable. If you want the result even if the grouptable has no rows, you can use this:

"SELECT DEFAULT( $group ) 
FROM (SELECT 1) AS dummy
LEFT JOIN grouptable
ON True
LIMIT 1 ;"

Set select default value with redux-form in React

Have you tried passing defaultValue prop to select?


const renderDropDownField = ({ input, label, values, defaultValue }) => (
<Container>
<Row>
<Col sm="6">
<label className="input-label">{label}</label>
</Col>
<Col sm="6">
<Row>
<select defaultValue={defaultValue} className="dropdown-list" {...input} >
{values.map((value, index) => (
<option key={value} value={value}>{value}</option>
))}
</select>
</Row>
</Col>
</Row>
</Container>

How to set default value for HTML select?

Note: this is JQuery. See Sébastien answer for Javascript

$(function() {
var temp="a";
$("#MySelect").val(temp);
});

<select name="MySelect" id="MySelect">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>

How to set a default value in react-select

I guess you need something like this:

const MySelect = props => (
<Select
{...props}
value = {
props.options.filter(option =>
option.label === 'Some label')
}
onChange = {value => props.input.onChange(value)}
onBlur={() => props.input.onBlur(props.input.value)}
options={props.options}
placeholder={props.placeholder}
/>
);

#EDIT 1 : In the new version

const MySelect = props => (
<Select
{...props}
options={props.options}
onChange = {value => props.input.onChange(value)}
onBlur={() => props.input.onBlur(props.input.value)}//If needed
defaultValue={props.defaultValue || 'Select'}
options={props.options}
placeholder={props.placeholder}
/>
);

How to set a default value for an existing column

This will work in SQL Server:

ALTER TABLE Employee ADD CONSTRAINT DF_SomeName DEFAULT N'SANDNES' FOR CityBorn;


Related Topics



Leave a reply



Submit