VBA .getElementbyID and passing value to an onchange javascript event - javascript

I have the following code which selects an item from the drop down by using the .getelementbyid method. The way the page works is once the item is selected, a new screen populates allowing to send a user a message to someone (the form uses an onchange event to handle when you select an item from the drop down list). When running this code, it selects the proper item but the page does not repopulate.
With HTMLDoc2
.getElementById("navbar1").setAttribute "value", "getaddr"
End With
How do you force the form to recognize that you have selected one of the drop down items and process the onchange event?

I was looking for a similar answer and found this that may be useful.
I have a SELECT element on a web page (which has an ID) that has an onchange javascript associated with it.
I accessed it as follows (using your example):-
dim objURLpage as MSHTML.HTMLdocument
dim appIE as InternetExplorer
dim objSELECTelement as MSHTML.HTMLSelectElement
'this type will depend on your HTML element type but usually dropdown lists are "OPTION" elements between "SELECT" elements
Set objURLpage = appIE.document 'I found this gives current web page that's open
set objSELECTelement = objURLpage.getElemebtById("navbar1")
objSELECTelement.selectedindex = 0
'number depends on position of item in dropdown list starting at "0"
objSELECTelement.FireEvent("onchange")

Related

(Django) How do I make the 'selected' option valid after altering a foreign key drop-down?

I have a model whose fields are a date and a foreign key to another model's text field:
# models.py
class Publications(models.Model):
"""A class for daily article publications"""
date = models.DateField(
help_text="date for this article to be published"
)
headline = models.ForeignKey(
Articles,
help_text='The article to be published, represented by its headline'
)
On the Admin 'Add' page for that model, I've added Javascript so that when I select a date from the calendar selector widget for date, the drop-down selector for the Articles FK is automatically restricted to the subset of Articles ready for publication on that day. This is accomplished by clearing the drop-down selector and then re-filling it with the results of an AJAX call to a backend view:
// Javascript for Publications "Add" admin page
// Grab the Article <select> element
let artSelect = document.getElementById("id_headline");
// Clear it
artSelect.innerHTML = '';
// Fill it with new <option> elements from 'data', a list of Article
// headlines retrieved via AJAX
for (var i=0; i<data.length; i++) {
var opt = document.createElement('option');
opt.value = i;
opt.innerHTML = data[i];
// If it's the first option, make it 'selected'
if (i == 0) {
opt.setAttribute("selected", "");
}
artSelect.appendChild(opt);
}
If, after selecting a date and watching the headline drop-down be filtered accordingly, I select one of the filtered set of headlines, I can select "Save" and the Publications entry (date and Article FK) will be saved to the database successfully.
The problem I've encountered is that if I don't manually select an Article from the drop-down after filtering by date -- for example, if the first headline showing in the new, filtered drop-down is the one I want -- and I try to select Save, the page reloads with a prompt to "Please correct the error below" and an error over the headline drop-down "Select a valid choice. That choice is not one of the available choices." The behavior is unique to the first option, the one shown by default; it happens even if I open the drop-down and manually click on that option, and it happens whether or not I give it the 'selected' attribute. As I mentioned above, if I open the drop-down and select any other option, the page works fine.
I've traced the error message to the ModelChoiceField() class of django/forms/models.py in the main Django installation. However, I'm not sure where to go from here. Not only do I not want to change a file in Django itself, I don't know how I would. I can't figure out what changes I can make within my own app to make the default Article selection valid.
One guess I can make is that the Admin is programmed to always treat the first value in a FK drop-down as a placeholder option, like "-----". Is this true, and is there a way to disable it with a setting in my App's admin.py? I tried looking for documenation about this, but I couldn't find any.
Otherwise, how do I make the default selection of a Foreign-Key dropdown a "valid choice" after altering the dropdown contents?
EDIT: I've decided it's better UX to leave the first option as a non-valid option that indicates what filtering has been done, so this question is moot for my purposes. However, I think the underlying question of "Does the Django Admin always treat the first value in a FK drop-down as a placeholder option, and is there a way to disable it?" is a good question, so I'm leaving this open.
Your mistake is quite the subtle: you set the value of the first option to 0. Foreign keys start at 1... So an option value of 0 can never be in the Model choices. And you'll probably find that selected articles do not correspond with the titles (or only by chance).
You should present a list of 2-tuples to your ajax (or objects with 'id' and 'title) and then create opt.value = data.id.

Passing value of select list through Jquery in Oracle Apex 4.2

The following classic report which when we select specific row pops up with new modal region called addExtraDetails with some data grabbed from row and some new additional info required from user:
When (+) is being clicked the new modal region pops up, with populated values taken from the report row. So: in Column link I put:
javascript:function_to_add_to_basket('E',#ID#, 'Extra#ROWNUM#', #PRICE#,'DUMMY');
Then external js function is responsible for passing information. The problem is it does not refresh every time (+) is populated instead it keeps values of the first input.
I found better solution(and cleaner), upon clicking (+) Column Link is passing:
javascript:$s('P4_SET_QUANTITY','#QUANTITY#');
javascript:$s('P4_SET_TYPE','E');
javascript:$s('P4_SET_OBJECT_ID','#ID#');
javascript:$s('P4_SET_ELEMENT_ID','Extra#ROWNUM#');
javascript:$s('P4_SET_COST','#PRICE#');
javascript:$s('P4_SET_DISCOUNT','DUMMY');
javascript:openModal('addExtraDetails');
Now it updates everytime when we select various rows HOWEVER since we have dropdown javascript grabs all possible value of Quantity column so for this code:
javascript:$s('P4_SET_QUANTITY','#QUANTITY#');
the output is: '012345678910'.
How can I pass all values to modal region and make it to work with new values every time its being called ?
You need to retrieve the value of the select list when you click the modal button. The value is not static, unlike the other values. The substitution strings are replaced with their value when the page is rendered.
It isn't really necessary to do all those item sets if all you want to do is use them in a javascript function. Your first idea was probably just as good but I'll just run with openModal.
First, determine how to target the select list. You did not specify whether your report is a wizard-generated tabular form or a manual tabular form (ie used apex_item to make the select list). You could either target the select list by the name attribute, which refers to an array, or select by header of the column. Also see this article.
Eg, with the column name for the select list being QUANTITY, selecting the list would be:
td[headers=QUANTITY] select:visible
Alternatively, if you determine the array you can be more precise in targetting the element. Eg if the NAME attribute is set to f02 then you could select the select lists with
input[name=f02]
Then modify the openModal function to select the list value on the same row as pThis - which would be the triggering element, the anchor :
function openModal(pThis, pMethod){
//fetch the value of the select list on the same row
//I use the second method of selecting here
var lListValue = $(pThis).closest('tr').find('input[name=f02]').val();
...
}
You'll also need to adjust your call to openModal:
javascript:openModal(this, 'addExtraDetails');

Determine which javascript function is being triggered on element change

I have a page that has two drop down lists. When the first dropdown list's value is changed, some function is triggered that updates the values of the second list depending on what is selected in the first list.
Is there some tool similar to FireBug, for example, that would tell me what function is being triggered by the updating of the drop down list? There is no "onChange" parameter in the Select element.
If I change the selected item manually by clicking on the element, this function is being triggered. But if I change the selected item programmatically using something such as:
MyElement.SetAttribute("SelectedIndex", 10)
the function to update the other field is not being fired. How can I find which function should be fired and how will I trigger it?
I am using VB.Net and loading/manipulating the page in a web browser control.
Not sure if it makes a difference, but the dropdown elemet has some attributes that I am not familiar with, such as "ng-model" and "ng-change".

DJANGO HTML TEMPLATES : How to know Selected item in drop Down and display In the html page without any action

I want to display the selected item info which is selected From the drop down in, the same html page without dong any action.How to know which item is selected and how to assign that selected value in a variable in html.
You can use jQuery's onchange event handler to detect when choice is changed and also get the value. There is good example on the .change() api page.

How to display title on html dropdown items for non-javascript browsers?

I am using ASP.Net MVC 2.0 with C#.
I have a html dropdown control on my page which may contain items with longer text. The page design does not allow me to increase the drop down width so that I can display entire text for each item. To overcome this, I am trying to display a title on mouse over of each item so that use can see the item text. This is achievable using javasctipt and I am happy with the solution. But what if the client has disabled javascript on his browser? Is there any way we can display title for each drop down item without depending on javascript? May be using the "title" attrbute of option or something simillar? I am targeting my code to work for IE8 clients.
Following is my current implementation. I have called a javascript function on mousemove. This loops through drop down options and assigns the selected value to the title attribute of selected option.
<%: Html.DropDownListFor(model => model.SelectedTeamId, new SelectList((IEnumerable)""), "Please Select", new { #class = "wid140", id = "QueryTeam", onmousemove ="ShowSelectedText(this);"})%>

Categories