EchO
EchO

Reputation: 1344

Jquery not getting full value from html

I have this:

$("#test").click(function() {
  alert($(".selected").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="selected" value="131cffb1-cff5-46c1-9bb1-865665c21ae8">weq Disponivel</li>
<button id="test">Button</button>

i want to be able to get the guid of the li by clicking that button, the problem is, i am unable to get it, can only get the first numbers like "131"

Thanks all

Upvotes: 1

Views: 64

Answers (4)

petar
petar

Reputation: 11

you can't get the full value of list item cause it accepts only numbers and it is accepted only in ordered lists. What you need to do is to use custom html attribute like data with your profix so you don't have issues later with some jquery plugin.

This is example.

weq Disponivel

var value = $('li').data('prefixValue');

It is important to note that jquery will take camelcase 'prefixValue' and convert it to the right format with dashes and append 'data-' on the beginning. I hope this helps.

Upvotes: 1

Jan_dh
Jan_dh

Reputation: 771

Do this with a data-attribute.

<li class="selected" data-value="131cffb1-cff5-46c1-9bb1-865665c21ae8">

and then u can get the value with

var value = $('li').data(value);

$('#test').on('click',function(){
              
       var test = $('li').data('value');
       alert(test);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="selected" data-value="131cffb1-cff5-46c1-9bb1-865665c21ae8">weq Disponivel</li>
<button id="test">Button</button>

Upvotes: 2

Sudharsan S
Sudharsan S

Reputation: 15393

Without using .data()

$("#test").click(function() {
   console.log($(".selected")[0].attributes[1].value);
   console.log($(".selected")[0].attributes[1].nodeValue);
   console.log($(".selected")[0].attributes[1].textContent);
});

Fiddle

Upvotes: 1

bhaskarmac
bhaskarmac

Reputation: 339

Please try below code:

alert($(".selected").attr('value'));

Upvotes: 2

Related Questions