0

I'm using a function like this to extract a few data from a form:

$(document).ready(function() { 
     $('#btnEnviar').click(function() {
            var email = $('#email').val();

It's working, but now I also need to extract the content of a list box, which is a mark sign.

So I put the list box inside a span with id "idaevolta":

      <ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: 0px; left: 0px; display: none;"></ul>
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: 0px; left: 0px; display: none;"></ul>

 <input id="cbIdaVolta" type="hidden" name="cbIdaVolta" />

So far so good, but when I try to extract the data:

var idaevolta = $("#idaevolta").html();

It is simply not working.

UPDATE

WHILE LOOKING AT THE HTML OUPUT, WITH CHROME CONSOLE, LOOK WHAT I FOUND:

<input type="checkbox" id="cbIdaVolta" name="cbIdaVolta" style="width: 35px; height: 35px; margin-left: 10px;">

Does it make things easier to find a solution?

UDPATE 2

It is actually a check box.

user3692451
  • 549
  • 1
  • 6
  • 20

4 Answers4

0

You have multiple elements with the ID "idaevolta" ... the span and the input elements. ID attributes must be unique.

Spechal
  • 2,634
  • 6
  • 32
  • 48
0

First of all, you can never set two different items with the same id. Only the first one will be recognized. Second, I would use some jquery to say 'if 1st ul is clicked, set #idaevolta value to something'. After this, you'd retrieve the value just like you've done with the email field.

https://api.jquery.com/click/

I hope that helps!

PS: having 'ul's without any 'li' in it just look weird... maybe you should try something different

  • I'm getting a bit confused here. I'm using 2 IDs for a hidden input and a span in a different part of the script and they are working just fine, why would this one be different? The form needs to be sent somehow. – user3692451 Apr 01 '15 at 02:43
  • 'working fine' is something accidental. Surely the code is only getting one of the elements. Now that you changed your approach to a checkbox, things will get prettier :) – Moroni Lemes Apr 01 '15 at 12:57
  • I believe this topic will solve your case: http://stackoverflow.com/questions/2204250/check-if-checkbox-is-checked-with-jquery – Moroni Lemes Apr 01 '15 at 12:58
0
  1. you can only have one ID in the document, even if they are different elements
  2. since you break that rule, you can $('span').html()
vol7ron
  • 40,809
  • 21
  • 119
  • 172
0

You should have a unique name for id, otherwise use class. ul element should have li. And if you want to get the data from a list. Try something like this:

var x = $('#idaevolta').find('ul li').text();
Sachi Tekina
  • 1,800
  • 5
  • 28
  • 50