db92
db92

Reputation: 193

How can I get selected value from a list-grop with jQuery?

I need to take some value (text and ID) from a selected value on a list-grop:

<div class="list-group col-lg-10" >             
    <a href="#" id="id1" class="list-group-item col-lg-6">Example 1</a>
    <a href="#" id="id2" class="list-group-item col-lg-6">Example 2</a>     
</div>

I want to take them only after click on a button. How can I get the values?

Upvotes: 1

Views: 2949

Answers (2)

P. Frank
P. Frank

Reputation: 5745

UPDATE 2

StoreValue = []; //Declare array
$(".list-group a").click(function(){
    StoreValue = []; //clear array
    StoreValue.push($(this).attr("id")); //add id to array
    StoreValue.push($(this).text()); // add text to array
})
 $(".button").click(function(){
    alert(StoreValue[0] + " " + StoreValue[1])
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list-group col-lg-10" >             
    <a href="#" id="id1" class="list-group-item col-lg-6">Example 1</a>
    <a href="#" id="id2" class="list-group-item col-lg-6">Example 2</a>     
</div>
<div class="button">Click here</div>

Upvotes: 2

guradio
guradio

Reputation: 15555

    $('a').click(function () {
    var id = $(this).attr('id')
    var text = $(this).html()


    console.log(id)
    console.log(text)
})

$('#get').click(function () {
    var id1 = $('#id1').attr('id')
    var text1 = $('#id1').html()

    var id2 = $('#id2').attr('id')
    var text2 = $('#id2').html()
    console.log(id1)
    console.log(text1)
    console.log(id2)
    console.log(text2)
})

create an event click and inside get the id using $(this).attr('id') and the text using $(this).html()

demo

Upvotes: 0

Related Questions