1

I'm trying to deal with one userscript user may have installed when he visits my site. As it works in a sandbox, I can't directly turn off some of the userscript's features that interfere with my site's native functionality. So the only option for me is to access them though the UI. First I needed to change one particular checkbox. First I tried to use jQuery's .val() method but it did not work. Then I did this:

if($('#someinput:checked').length) $('#someinput')[0].click();

And then it worked, the unwanted feature got turned off. Next one was <select>. Just .val() did'n help as expected, so I also triggered the change event right after I changed the value:

$('select[name=someselect]').val(0);
$('select[name=someselect]')[0].change();

But it still does not work. I guess I need to trigger change event somehow more properly. How do I do that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Juribiyan
  • 700
  • 8
  • 25

2 Answers2

0

You need to use:

$('select[name="someselect"]').change();

instead of:

$('select[name=someselect]')[0].change();

because $('select[name=someselect]')[0] return a DOM element, not a jQuery object.

Felix
  • 37,892
  • 8
  • 43
  • 55
  • I don't get what you means. Can you rephrase it? – Felix Mar 28 '14 at 08:18
  • Sorry, I just tested that again and found out that with checkbox both `$('#someinput')[0].click();` and `$('#someinput').click();` worked. Still `$('select[name="someselect"]').change();` doesn't. – Juribiyan Mar 28 '14 at 08:22
0

I found a solution here on SO:

Basically as far as I understand it fires all the native events properly and it's neatly wrapped into a jQuery plugin.

Community
  • 1
  • 1
Juribiyan
  • 700
  • 8
  • 25