Jump to content

JS command that selects an option


weber

Recommended Posts

:)what do you mean?edit: are you looking to find how to attach event handlers to dropdown's when an option is selected?

Link to comment
Share on other sites

why bother with the drop down? Just make a function which creates an array of hard coded pages to go to, pick a random number from 0 to the length of the array - 1, and then redirect to it.

Link to comment
Share on other sites

well, as I sad in my first post I did say i wasn't sure what you were talking about, so most of this has been guess work. What about what else I said in my first post, about attaching an event handler to a select tag? Is that what you want? :)Could you please elaborate on what exactly you trying to do?

Link to comment
Share on other sites

Not sure if this is what you want. Run a loop through the list of options in a select box and match the text value with the value you want to set. When the value matches the option, set the selectedIndex. Here's a simple function to set the selected index:

function setSelectedIndex(list, value){   for(i=0; i<list.length; i++){	  if(list[ i].value == value){		 list.selectedIndex = i;		 break;	  }   }}

Call the function with this:setSelectedIndex(document.someform.somelist, 'Jim');This would set the select list named "somelist" to show the option "Jim" (if "Jim" exists in the option list). Crude but effective.

Link to comment
Share on other sites

let's say we have this form in a webpage (not a webpage that I build, but a webpage that I browse):<form id="myForm" id="mySelect"> <option>Apple</option> <option>Pear</option> <option>Banana</option> <option>Orange</option></select></form>I want, whenever I visit this webpage that contain this form to select "Orange" automatically, without me having to open the drop down menu and click on "Orange"is there a command like this? document.forms['FORMNAME']FIELDNAME.selected='selected';or some kind of command that will do it?the script with the looping seems effective, but I thought there would be a simple command that can do itall I need is to replace someform with myForm ?

Link to comment
Share on other sites

The only way I can think of to do this would be with a GreaseMonkey script. Or perhaps Firefox allows some way of attaching arbitrary JS code to a profile, but even then I don't think it would extend down to the site or page level.

Link to comment
Share on other sites

yes, but that's not what you're asking for. You're looking for a browser utility.

Link to comment
Share on other sites

yes, but that's not what you're asking for. You're looking for a browser utility.
by "browser utility" you mean an external software application? and not a JS script (userscript, greasemonkey script, whatever you call it)? I don't think so...you mean that it is not possible for a JS script to automatically perform actions on DOM elements?I am pretty sure that JS script CAN autoclick a specific button in a specific website in Opera browser, selecting a dropdown menu entry is what it cannot do? those two actions seem similar to me, if JS can do the former, I bet it can do the latteram I in the wrong forums or no one undestards what I am talking about?
Link to comment
Share on other sites

show me how that autoclick script works (link/code) so some of us can actually see what you're talking about.

Link to comment
Share on other sites

show me how that autoclick script works (link/code) so some of us can actually see what you're talking about.
one example is this:
// ==UserScript==// @include http://*.website.com/*// ==/UserScript==document.addEventListener('DOMContentLoaded',function() { var identifier = "Download" var attrs = ['action','defaultValue','value','name','src','','='+identifier+'" or @]; attrs = ('@'+attrs.join(attrs.pop())).slice(0,-5); var lnk = document.selectSingleNode('//a[@href='+identifier+'" or text()="'+identifier+']|//button['+attrs+' or text()='+identifier+']|//input[(@type="submit" or @type="image" or @type="button") and ('+attrs+')]'); if( lnk ) { lnk.click(); }},false);
it selects a node I think, and it clicks a button
Link to comment
Share on other sites

The technical aspect of what you ask is entirely possible, using JavaScript - End User showed you some functioning code. All you have to do is find a way to run it on-demand in the Opera browser. We don't know how that will work, as Opera does not have a comparable system to GreaseMonkey (not having extensions and all), so you may have more luck on the Opera forums.

Link to comment
Share on other sites

to run it on-demand in the Opera browser
I think, this purpose is served by this code:document.addEventListener('DOMContentLoaded',function()the 'demand' to run it, is to actually visit that websiteso, I suppose End user's loop is not really necessaryI read about the selectedIndex command:"The selectedIndex property sets or returns the index of the selected option in a dropdown list."I dont think this is what I want, what I want is to "set an option as selected" or this is not the way it works?
Link to comment
Share on other sites

I dont think this is what I want, what I want is to "set an option as selected" or this is not the way it works?
Read carefully: "The selectedIndex property sets or returns the index of the selected option in a dropdown list."Yes, you can use the selectedIndex property to set an option list to a particular value. This would typically be done by JS code in the page that's running in the browser. If the page doesn't already contain that code, then you would have to add it to the page dynamically using some kind of utility like GreaseMonkey.
I don't think this is so hard....
Lots of things don't look hard when you don't understand them.
Link to comment
Share on other sites

Are you really going to split hairs over shorthand grammar!? :)

then it should write "The selectedIndex property returns the index of the selected option in a dropdown list or sets the index of an option in a dropdown list as selected"
is that not what this:
"The selectedIndex property sets or returns the index of the selected option in a dropdown list."
is saying?
Link to comment
Share on other sites

okiesomething else, why do I have to make a loop for the JS to read all the entries, can it just read them as the webpage loads? and me simply make the JS point to the right entry, if it exists?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...