Reputation: 1344
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
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
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
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);
});
Upvotes: 1