Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    14
    ReputationRep:
    Hello.

    I would like to change a certain website that I use a lot to take certain elements out of a rather long drop-down list so that there are only the few that I use.

    So, for instance:

    <select>
    <option>A</option>
    <option>B</option>
    <option>C</option>
    <option>D</option>
    <option>E</option>
    <option>F</option>
    <option>G</option>
    <option>H</option>
    <option>I</option>
    </select>

    I would like to change it to:

    <select>
    <option>B</option>
    <option>F</option>
    <option>G</option>
    </select>

    Can anyone help with this?
    Offline

    2
    ReputationRep:
    Okay I just tested this under Greasemonkey and Firefox and it works:

    Code:
    var keep = ["B", "F", "G"];
    var select = document.getElementById('my_list');
    
    // Do not edit below
    
    function containsItem(arr, value) {
    	var l = arr.length;
    
    	while (l--)
    		if (arr[l] == value)
    			return true;
    
    	return false;
    }
    
    var i = 0;
    
    while (select.options.length != keep.length) {
    	if (!containsItem(keep, select[i].value)) {
    		select.remove(i);
    		i = 0;
    	} else {
    		++i;
    	}
    }
    As you can see there is a variable called keep, it contains the elements you want to keep.
    You can add more by separating new elements with commas and making sure they are written within double quotation marks.
    However, you need to ensure that the elements to keep exist in the drop-down list, character by character + case sensitive (copy paste them from the HTML source to guarantee they are correct).
    NOTE: The elements to keep HAVE to exist in the drop-down list or else you will end up in an infinite loop.

    Lastly, to set it up you need to select your drop-down list, to do this look at the HTML source of your target drop-down list and chances are that it will say something like:

    <select id="dropdownlist">

    You want its ID, and replace it with the one in the select variable, so in this case:

    var select = document.getElementById('my_list ');

    becomes

    var select = document.getElementById('dropdow nlist');

    If there isn't an ID tag, then things become complicated... you will have to experiment with the following code:

    var select = document.forms[0].elements[0];

    Where 0 refers to the first form in the HTML page and the second 0 refers to the first HTML form tag in that form.
    (You'll need to experiment with the values and look at the source to get it working)

    You can copy paste the above code directly into a new Greasemonkey script (making the changes as explained above of course) and it should work fine.

    Anyways, if you can't get it to work, then post the URL of the page and identify the drop down list and I'll see what I can do.

    Hope that helps.
    • Thread Starter
    Offline

    14
    ReputationRep:
    (Original post by darkraver)
    ...
    It doesn't seem to work quite right. It removes all the items out of the drop-down list apart from four, which is what I wanted, but it keeps the wrong four. It keeps the last four items of the list.
    Offline

    2
    ReputationRep:
    (Original post by MSB)
    It doesn't seem to work quite right. It removes all the items out of the drop-down list apart from four, which is what I wanted, but it keeps the wrong four. It keeps the last four items of the list.
    That only happens when the elements to keep have been incorrectly specified.

    For example you may see the elements in the list as:

    Google
    something
    Test

    Whilst in reality (the HTML code) may be:

    <option>Google </option>
    <option>something </option>
    <option> Test</option>

    So your keep variable should be:

    var keep = ["Google ", "something ", " Test"]; (notice the extra spaces)

    As I said, the elements have to match the ones to keep in the drop-down list character by character, or it will remove the last x elements instead. To 100% guarantee you get the right element values, you should copy-paste them directly from the HTML source.

    If you don't mind, post the URL to the page and the elements you want to keep and I'll write a working script.
    • Thread Starter
    Offline

    14
    ReputationRep:
    (Original post by darkraver)
    That only happens when the elements to keep have been incorrectly specified.

    For example you may see the elements in the list as:

    Google
    something
    Test

    Whilst in reality (the HTML code) may be:

    <option>Google </option>
    <option>something </option>
    <option> Test</option>

    So your keep variable should be:

    var keep = ["Google ", "something ", " Test"]; (see the extra spaces)

    As I said, the elements have to match the ones to keep in the drop-down list character by character, or it will remove the last x elements instead.

    If you don't mind, post the URL to the page and the elements you want to keep and I'll write a working script.
    I had made a mistake when I posted before, but I have gone over it again carefully and it still does the same. I will PM you.
 
 
 
Reply
Submit reply
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Updated: January 23, 2010
Poll
Do you agree with the PM's proposal to cut tuition fees for some courses?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.