If I have accessed a Select element on a website, how would I go about changing it's selected item?
I tried:
selectElement.SetAttribute("value", "30");
But this doesn't work.
Thanks!
With this html
<select id="a">
<option value="1">1</option>
<option value="2">2</option>
</select>
Doing this will select the second option
document.getElementById("a").selectedIndex = 1
see this jsfiddle example: http://jsfiddle.net/vJsvV/
Related
Good morning.
I can not select an item from the drop-down list.
Did the following:
1) I entered the text
WebBrowser1.Document.GetElementById ("Advert_category_id").InnerText = "Wedding / Event Services";
2) Set the value using the following commands, in different sequences, as soon as not twisted.
WebBrowser1.Document.GetElementById("Advert_category_id").SetAttribute("selected", "selected");
WebBrowser1.Document.GetElementById("Advert_category_id").InvokeMember("onchange");
WebBrowser1.Document.GetElementById("Advert_category_id").InvokeMember("submit");
WebBrowser1.Document.GetElementById("Advert_category_id").InvokeMember ("value", "84");
The problem is that this code selects the option I need, but it does not appear in the field. Those. Only when you click on the field, after executing the code, you can see that the desired option is selected. But this approach is also not true, because
The following drop-down list named SUB CATEGORY does not load the required data (the data remains with the category selected by default - Professional)
3. I tried to experiment with different id-shniki. Nothing helps :(
The site itself https://list.asiandirectoryapp.com/advert/create
(You must register to view)
I made the second ack,
That would not waste time for registration
fovogaze#p33.org
qwerty123
The code of interest
<div style="display:none"><div class="form-group"><label class="control-label" for="Advert_city_id">City</label><select class="form-control" name="Advert[city_id]" id="Advert_city_id">
</select><div class="help-block error" id="Advert_city_id_em_" style="display:none"></div></div></div><div class="form-group"><label class="control-label required" for="Advert_category_id">Category <span class="required">*</span></label><select class="form-control" name="Advert[category_id]" id="Advert_category_id">
<option value=""></option>
<option value="35">Beauty, Health & Fitness</option>
<option value="19">Education</option>
<option value="21">Entertainment</option>
<option value="27">Food, Drink & Sweet Centres</option>
<option value="39">Home Services</option>
<option value="116">Jobs/Careers</option>
<option value="43">Motoring</option>
<option value="50">Other Services</option>
<option value="1">Professional</option>
<option value="51">Property Improvements</option>
<option value="84">Wedding/Event Services</option>
<option value="117">Whats On Guide</option>
</select><div class="help-block error" id="Advert_category_id_em_" style="display:none"></div></div><div class="form-group"><label class="control-label required" for="Advert_categoryList">Sub category <span class="required">*</span></label><select multiple="multiple" class="form-control" name="Advert[categoryList][]" id="Advert_categoryList">
<option value="2">Accountants</option>
<option value="4">Claims</option>
<option value="123">Clothing Manufacturers</option>
<option value="6">Estate & Letting Agents</option>
<option value="7">Finance/Mortgages</option>
<option value="8">Graphic & Web Design</option>
<option value="9">Immigration</option>
<option value="10">Information Technology</option>
<option value="11">Insurance</option>
<option value="13">Mobile Apps - Mobile Phones</option>
<option value="14">Online Marketing/ SEO</option>
<option value="18">Print</option>
<option value="15">Retail and Wholesale</option>
<option value="16">Solicitors - Lawyers</option>
<option value="17">Travel Agents</option>
</select><div class="help-block error" id="Advert_categoryList_em_" style="display:none"></div></div><div class="form-group"><label class="control-label required" for="Advert_name">Company Name <span class="required">*</span></label><input class="form-control" placeholder="Company Name" name="Advert[name]" id="Advert_name" type="text" maxlength="100" /><div class="help-block error" id="Advert_name_em_" style="display:none"></div></div>
P.S. I can fill out all the fields, press the buttons the same way, but with the drop-down list the trouble. Thank you for any help.
I had a similar problem but I chose to solve it by simply invoking a client-side javascript (with a parameter) that handles the manipulation of the dom objects.
I found that this way I can separate code too, keeping dom handling in the JS and application-code in the winforms. Itll also allow you to leverage any functionality provided by e.g. jQuery plugins wrapping the select.
It could look like this (not tested):
Object[] objArray = new Object[2];
objArray[0] = selectorID;
objArray[1] = valueToSet;
webBrowser1.Document.InvokeScript("SetSelector", objArray);
and in the client side:
function SetSelector(selectorID, valueToSet){
$('#'+selectorID).val(valueToSet); //or any function you'd like
}
I am trying to create a multiple choice select field in my .cshtml file that looks like this:
<select multiple="multiple" id="select-multiple" style="width:500px" name="MultipleSelect">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
</select>
I have a model that has a List<string> MultipleSelect that maps perfectly when i save my model. all the selected values get to the controller and are saved in the database. But when the page reloads the values don't show. I checked with the debuger and the MultipleSelect list gets filled with the values before the view gets loaded.
Should I use a diferent type for my MultipleSelect value or is this just not the right way to do this?
Im trying to write an application where web form will be filled with one click. The problem is that I can't get working picking one option from listbox, here is HTML code I'm facing:
<select name="data[PvpnetAccount][date_of_birth_day]" id="PvpnetAccountDateOfBirthDay" class="ignore_keyup c-signup__input c-signup__input--select c-form-group__input c-form-group__input--select js-selectize">
<option value="" disabled selected >Day</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
I have tried with different options, but nothing gave me results so far, so I'm up for any suggestions. My first try was:
webBrowser1.Document.GetElementById("PvpnetAccountDateOfBirthDay").SetAttribute("value", "03");
However Im aware that it won't work.
EDIT:
A guy advised me to do this like this:
var daySelect = WebBrowser1.Documents.GetElementById("PvpnetAccountDateOfBirthDay");
var day10Option = daySelect.Children.Single( e => e.Value == "10" );
day10Option.SetAttribute("selected", "selected");
However it seems like .Single gives me error.
Just as a precaution for test purposes to make sure that page loaded correctly and you got correct element selected look at outerHTML property of the element. Also, are you executing this code on background worker, if yes you will need to use Action let me know if you need more instructions on that. Also try using IHTMLSelectElement instead of HtmlElement. To use it add reference to
Microsoft.mshtml
and add using mshtml; to your file. After you can do the following:
HtmlElement tmp = webBrowser1.Document.GetElementById("PvpnetAccountDateOfBirthDay");
IHTMLSelectElement tComboBox = (IHTMLSelectElement)tmp.DomElement;
tComboBox.selectedIndex = 2;
Hope that helps.
In asp.net mvc you can use:
#Html.DropDownListFor(model => model.Category, ViewBag.Category as IEnumerable<SelectListItem>, "-- SELECT --", new { #class = "form-control" })
Using asp.net 5, how do I include the default or null value (-- SELECT --) in a taghelper:
<select asp-for="Category" asp-items="#ViewBag.Category" class="form-control"></select>
You can just insert an option item inside the select:
<select asp-for="Category" asp-items="#ViewBag.Category" class="form-control">
<option disabled selected>--- SELECT ---</option>
</select>
The disabled keyword in the code above means that the "--- SELECT ---" row cannot be picked again once a choice has been selected in the list. If you want the user to be able to blank the selection again (i.e. if it's bound to a nullable field) then just omit disabled.
If you want to store value null to database then use
<option selected value="">Full Access</option>
If you use asp-items for your model , It uses value 0 for selected item , If you have tag <option> with your default string without value ,
You must use a loop for <option> and put your default option out of loop.
This is an example for .Net Core using asp-items:
<select asp-for="DepartmentUnit" asp-items="#Model.DepartmentUnits" class="form-control"></select>
and Now this is another example for using loop:
<select>
<option val="">--Select--</option>
foreach(item in Model.DepartmentUnits)
{<option val="#item.val">#item.title</option>}
</select>
This answer by Matt:
<select asp-for="Category" asp-items="#ViewBag.Category" class="form-control">
<option disabled selected>--- SELECT ---</option>
</select>
also works with validation. Probably due to the option being disabled it seems not to be a valid selection.
btw.: this also works with MVC Core 3 ;-)
Html code/example of dropdown:
<select class="ng-valid ng-dirty ng-touched" style="max-width: 235px" ng-model="blogerj" ng-options="blogerj.long_name for blogerj in blogerj track by blogerj.id">
<option class="" value="">[Choose...]</option>
<option label="label 1" value="1">Text Content 1</option>
<option label="label 2" value="2">Text Content 2</option>
<option label="label 3" value="3">Text Content 3</option>
<option label="label 4" value="4">Text Content 4</option>
</select>
If I just set attribute making it selected then it dont work because button for submit appears only when element is selected.
I also tried just to click element to open dropdown and then again click desired option but again it gave me no results.
foreach (GeckoHtmlElement geckoHtmlElement in gwBrowser.Document.GetElementsByTagName("select"))
{
if (geckoHtmlElement.GetAttribute("class") == "ng-valid ng-dirty ng-touched")
{
geckoHtmlElement.Click();
}
}
Actually it finds element but clicking dont do anything.
So maybe there is any fancy js method to select that dropdown?
From the portion class="ng-valid ng-dirty ng-touched" it looks like there is AngularJS at work here - because angular usually uses the prefix ng-. Angular can intercept user actions and take control of how your web page controls behave.