Search code examples
javascriptimacros

Imacros get number of options in dropdown list


I am creating an imacros script to select a random option inside an html select element.

Like this:

mcr +="TAG POS=1 TYPE=SELECT ... CONTENT=#"+opcionAleatoria(12)+ '\n';

Where opcionAleatoria() is a function to grab a random number:

And total is the total amount of options inside that select.

function opcionAleatoria(total) {
return Math.floor(Math.random() * total) + 1;
}

I want to be able now to pass the total (amout of options in select) as a parameter to the function.

Why?

Two reasons:

  • So I don't need to count.
  • So it will work on combo boxes.

I tried passing document.form.select_id.options.length, but it won't work since in imacros document is not defined.

Do you have any ideas on how I may approach the case?


Solution

  • Try to modify your expression like so:

    var total = window.document.forms[0].select_id.options.length;
    


    If the suggested above way doesn't work, here's more reliable one:

    iimPlay("CODE:TAG POS=1 TYPE=SELECT ... EXTRACT=TXTALL");
    var total = iimGetExtract().split("[OPTION]").length;
    

    (I suppose that you use 'iMacros for Firefox' and its Scripting Interface.)