views:

193

answers:

3

Hi all, i'm new here. i've a question related to greasemonkey.

A page contain multiple radio buttoned values and one choice is to made, this correct choice option is hidden within the page

the radio buttons are present in the form whose structure is

<form name="bloogs" action="/myaddres.php" id="bloogs" method="post" >

then the hidden field as

<input type=hidden value="abcd" name="ans">

then all the radio button values are followed as

<input  type="radio" id="r1" name="opt" value="abcd"> abcd
<input  type="radio" id="r2" name="opt" value="efgh"> efgh
<input  type="radio" id="r3" name="opt" value="ijkl"> ijkl

and so on

thus i need the button with value=abcd be 'checked' as soon as the page loads. Thanks

A: 

I haven't done greasemonkey, but this may help:

use jQuery and do

$('[value="abcd"]').click()

Good luck.

Drew LeSueur
+2  A: 

There are some ways you can use:

1 You can pre-select it by putting in selected="selected" like this:

<input type="radio" id="r1" name="opt" value="abcd" checked="checked" /> abcd

2 You can use jQuery to do it easily (I don't know whether it will be applicable in terms of greasmonky though)

$(function(){
  $('input[value="abcd"]').attr('checked', 'checked');
});

3 You can loop through all elements and selected the one with raw javascript

var form = document.getElementById('bloogs');

for(var i=0; i<form.elements.length; i++)
{
  if (form.elements[i].type == 'radio')
  {
    if (form.elements[i].value == 'abcd')
    {
      form.elements[i].setAttribute('checked', 'checked');
      break;
    }
  }
}

Update:

This uses jquery and selects a radio after reading the value from hidden field:

$(function(){
  $('input[value="' + $('#hidden_field_id').val() + '"]').attr('checked', 'checked');
});

Or with raw javascript:

var form = document.getElementById('bloogs');
var hidden_value = document.getElementById('hidden_field_id').value;

for(var i=0; i<form.elements.length; i++)
{
  if (form.elements[i].type == 'radio')
  {
    if (form.elements[i].value == hidden_value)
    {
      form.elements[i].setAttribute('checked', 'checked');
      break;
    }
  }
}

Update 2:

As per the name, here is how you can go about:

$(function(){
  $('input[value="' + $('input[name="ans"]').val() + '"]').attr('checked', 'checked');
});
Sarfraz
I dont know jscript. also need the code to work in greasemonkey. I suppose gm supports only javascript code.
muqtar
@muqtar: yes js (jquery) is supported by gm.
Sarfraz
ok. but as i said the value changes in every new page so putting value=abcd doesn't work. as i said that the value from hidden feild has to be extracted then the radio button corresponding to it has to be checked.
muqtar
@muqtar: see my updated answer plz.
Sarfraz
To select radio button you have to add `checked` attribute, not `selected`
dev-null-dweller
@OP: Yes, GreaseMonkey scripts are written in Javascript (http://en.wikipedia.org/wiki/Greasemonkey#Technical_details). jQuery is not Javascript, it's a Javascript *library* that makes some things more convenient. It is *possible* to use jQuery in a GreaseMonkey script, but you have to make a special effort to do so (http://stackoverflow.com/questions/859024/how-can-i-use-jquery-in-greasemonkey).
T.J. Crowder
@muqtar: See my @OP comment above (flagging it up this because "@OP" isn't going to trigger the automatic notification, but "@muqtar" will -- not enough coffee this morning)
T.J. Crowder
@sarfraz: the input field does not have any id as you have mentioned 'hidden_field_id' in the code. i've mentioned the hidden input tag. The code doesn't work after i run in gm.Thanks.
muqtar
Please help me out....!!!
muqtar
Anybody knowing javascript or greasemonkey please try it.. Update or put in your implementation.. Plz..
muqtar
@muqtar: what is common in your hidden field, does it have the same name?
Sarfraz
yes the name is always "ans" and the value keeps changing in every new page.
muqtar
@muqtar: see my update 2 please.
Sarfraz
A: 

If you're trying to use jQuery with GreaseMonkey you're going to have to get good at writing delayed and try / retry type code. You need to start with something like this:

var _s1 = document.createElement('script');
_s1.src = 'http://www.ghostdev.com/jslib/jquery-1.3.2.js';
_s1.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(_s1);

That loads jQuery into your page. Next, you set something up that operates like so:

function dojQueryStuff() {
  if (jQuery == undefined) {
    setTimeout(dojQueryStuff, 1000);
  } else {
    // In here is where all of my code goes that uses jQuery.
  }
}

dojQueryStuff();

You've defined a function that'll check for jQuery's presence, and if it doesn't find it try again 1 second later. That gives the script time to load. Please note, if you don't use your own copy of jQuery the one listed in my example does not provide a $ variable. At the end of the script I have var $j = jQuery.noConflict();, so you'll access the jQuery functionality via $j or jQuery.

g.d.d.c
this is no longer necessary with current versions of greasemonkey - you can now use @require to load libraries when your script is installed.
rampion
I was unaware of this. Thanks!
g.d.d.c
Cant understand the jquery at all.. either put in the entire jscript greasemonkey code or in javascript gm code. Thaks
muqtar