Fill a multiple select with CasperJS

893 views
Skip to first unread message

Anne-Sophie Tranchet

unread,
Jun 5, 2013, 10:40:22 AM6/5/13
to casp...@googlegroups.com
Hi, 

I'm trying to fill a multiple select with CasperJS but I can't find the right way to do it.
I would imagine this to work :

@fill "form#edit_form", {
        "stores[]"   : [12, 13, 24]
 }, true

But it selects nothing.

However, if the array contains only one value, it works :

@fill "form#edit_form", {
        "stores[]"   : [12]
 }, true


Do you have any idea to make it work ?

I'm sorry I don't have a script ready to be tested online, I'll try to make one if it can help.


Have a good day,

AS

Nicolas Perriault

unread,
Jun 6, 2013, 4:47:33 AM6/6/13
to casp...@googlegroups.com
On Wed, Jun 5, 2013 at 4:40 PM, Anne-Sophie Tranchet
<as.tr...@gmail.com> wrote:

> Do you have any idea to make it work ?
>
> I'm sorry I don't have a script ready to be tested online, I'll try to make
> one if it can help.

I think it's not supported atm. Could you please file an issue about it?

Thanks

--
Nicolas Perriault
https://nicolas.perriault.net/
Phone: +33 (0) 660 92 08 67

Ven Karri

unread,
Aug 4, 2013, 2:35:36 AM8/4/13
to casp...@googlegroups.com, nic...@perriault.net
This is a very important feature to have. Any ideas would be greatly appreciated.

Robert Igl

unread,
Aug 4, 2013, 1:35:36 PM8/4/13
to casp...@googlegroups.com
What happens if you set the value of the mutliple select more times in an each/eachThen loop?
Does it only apply one at the same time or will they stay selected?

Anne-Sophie Tranchet

unread,
Aug 5, 2013, 9:26:25 AM8/5/13
to casp...@googlegroups.com, nic...@perriault.net
This is what I did. It's kind of ugly, but that's the only thing I could make work :


# We use evaluate and not fill because of one stupid multipleselect
@evaluate (data, store_ids) -> 
  document.querySelector('input[name="title"]').value      = data['title']
  document.querySelector('input[name="url"]').value        = data['url']
  document.querySelector('textarea[name="content"]').value = data['content']

  # Store ids multiple select 
  options = document.querySelectorAll("#page_store_id option")
  # Deselect all values
  i = 0; len = options.length
  while i < len
    options[i].selected = false
    i++
  # Select the one we want
  j = 0; len1 = store_ids.length
  while j < len1
    document.querySelector('#page_store_id option[value="'+store_ids[j]+'"]').selected = true
    j++
  # Submit form
  editForm.submit();
, data : data, store_ids : store_ids


So, for the explanation : I used evaluate instead of fill for fill my form. Filling half of it using @fill and the other half using @evaluate didn't work, that's why is do things like :

  document.querySelector('input[name="title"]').value      = data['title']
  document.querySelector('input[name="url"]').value        = data['url']
  document.querySelector('textarea[name="content"]').value = data['content']

As for the next part of the code, it's already documented : first you need to deselect every option, and then select the one that you want. I'm sure it can be done in a nicer way but I'm not a coffee expert (nor a javascript !)

Anyway, I hope that helps !

Gopi Akkumahanthi

unread,
Feb 21, 2014, 2:14:25 AM2/21/14
to casp...@googlegroups.com, nic...@perriault.net
Hi ,

will you send the same code for multiple select in javascript
Reply all
Reply to author
Forward
0 new messages