Crawlicious | tools for web business

CAT | programming

Supposedly you should be able to select select options by their text values like this

$(‘#myselect’).val(‘aval’);

This doesn’t seem to work if your options have value attributes however.

So, to select options by their text value, when you have values set (like this)

<select id=”myselect”><option value=”1″>alpha</option><option value=”2″>beta</option></select>

You can use a filter like this

$("#single option").filter(function() { return this.text == 'sometext'; }).attr('selected', 'selected');
 

Here is a whole example... The html was borrowed from some other place where they were explaining this, but they forgot the fact it doesn't work if you add values.
<select id="single">
    <option value="1" >Single</option>
    <option value="2" >Single2</option>
    <option value="3" >Single3</option>
    <option value="4" >Single4</option>
    <option value="5" >Single5</option>
    <option value="6" >Single6</option>
    <option value="7" >Single7</option>
    <option value="8" >Single8</option>
    <option value="9" >Single9</option>
</select>
<script>
    $("#single option")
      .filter(function() { return this.text == 'Single7'; })
      .attr('selected', 'selected');
</script>

No tags Hide

When doing unittesing in python it can get really annoying to compare dictionaries using the unittest.TestCase functions.  If they fail, the output is very messy, so I wrote these functions to help do the comparison.  Actually, there are a bunch of functions that I made to make testing a lot better in python.  You can download the file from TestAssertions.py.

Here is an example of what you can do with it…


self.assertEqualLists(l1, l2)

self.assertEqualDictionaries(d1, d2)

self.assertEqualDoms(d1, d2)

self.assertEqualMoney(m1, m2)

In addition to these helpful functions that produce much nicer output than the ugly self.assertEquals, I have also included  a really great class that will gather all test cases into a suite automatically.

Here is how you use that….


def suite():
 return DynamicTestSuite(modulename=__file__, namespace=globals(), skiptests=['MyCommonTest']).makeSuite()

One more thing, I used to try to pretty print deeply nested dictionaries and lists, but now I just use the following to get a really nice representation of deeply nested lists and dictionaries.  pprint is now my friend!


import pprint
 print "out=%s"%pprint.pformat(output)
 print "exp=%s"%pprint.pformat(expected)

Have fun!

Hide

Mar/10

9

awesome jquery manual

Here is a link to an awesome jquery manual that is so easy to use.  Thanks Remy and Yehuda!

Visual JQuery

, Hide

« Previous Entries

Next Page »

Find it!

Theme Design by devolux.org