1

I have ul like this:

<ul class="options" id="selt_count">
  <li value="1">One</li>
  <li value="2">Two</li>
  <li value="5">Five</li>
  <li value="12">Other</li>
</ul>
<input type="submit" id="send1" value="send">

i used this script to get all values of li into variable in the following format (1,2,5,12) in jquery:

<script>
    $(document).ready(function(){
        $("#send1").click(function(){
            var values = $('#selt_count li').map(function() {
                return this.value
            });
            values;
            var s = '(' + values.get().join(',') + ')';
            alert (s);
        });
    });
</script>

But this script is not working if the values of li are given as character

Example: Now i have a ul:

<ul class="options" id="selt_count">
    <li value="jonh">One</li>
    <li value="jack">Two</li>
    <li value="black">Five</li>
    <li value="barbosa">Other</li>
</ul>

the script will display (0,0,0,0)

Can anyone please help me and tell me what is the error? thank you in advance.

0

4 Answers 4

1

Try your HTML like this:

<ul class="options" id="selt_count">
    <li data-value="jonh">One</li>
    <li data-value="jack">Two</li>
    <li data-value="black">Five</li>
    <li data-value="barbosa">Other</li>
</ul>

And JavaScript like this:

<script>
$('#selt_count li').click(function() {
    var index = $(this).index();
    var text = $(this).text();
    var value = $(this).attr('data-value');
    alert('Index is: ' + index + ' , text is ' + text + ' and value is ' + value);
});
</script>

more about data-* here: http://html5doctor.com/html5-custom-data-attributes/


additional script based on your edits...

<script>
    $(document).ready(function(){
        $("#send1").click(function(){
            var values = $('#selt_count li').map(function() {
                return $(this).attr('data-value');
            });
            var s = '(' + values.get().join(',') + ')';
            alert (s);
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain me why 'value' was not working and why 'data-value' worked?
value attribute is not meant to be with <li>... it's for form elements... as to why data-value, you can read the link I provided...
1

Instead of getting text get the value attribute using .attr().

var text = $(this).attr('value');

Comments

0

Your usage of .index() is wrong. Check out the documentation: .index()

1 Comment

var index = $('#selt_count li').index(this);
0

Try this link. This may help you.

with,

$('#selt_count li').click(function() {
    var index = $(this).attr('value');
    var text = $(this).text();
    alert('Index is: ' + index + ' and text is ' + text);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.