Monday, May 20, 2024
121
rated 0 times [  122] [ 1]  / answers: 1 / hits: 23485  / 9 Years ago, mon, january 4, 2016, 12:00:00

I'm stuck with html/JS and have no idea how I should handle some events.
For example I have a list-group:



<div class=col-lg-3 col-md-3 col-sm-3 opciones>
<div class=list-group>
<a href=# class=list-group-item active>
Tokyo
</a> // barfoobarfoo
<a href=# class=list-group-item>London</a> // foo
<a href=# class=list-group-item>Paris</a> // bar
<a href=# class=list-group-item>Moscow</a> // foobar
<a href=# class=list-group-item>NY</a> //foobarfoo
</div>
</div>


What I want to do is:



1)change active elements on click.



2)call JS function when element is clicked.
UPD:
So now I know that click events can be handled with JQuery thing.



What I can't understand is to how to determinate which element is clicked. For example JQuery:



$('.list-group-item').on('click', function() {
$this = $(this);

$('.active').removeClass('active');
$this.toggleClass('active')

function simpleFunc(someargument) { //value of this argument depends on clicked item and can be (foo|bar|foobar ) etc
document.write(someargument) // here whould be written some text (foo|bar|foobar ) etc
})


There are no tutorials or anything, except this HTML code.
Thanks


More From » twitter-bootstrap

 Answers
72

You can simply use jQuery for this.



For example:



$('.list-group-item').on('click', function() {
var $this = $(this);
var $alias = $this.data('alias');

$('.active').removeClass('active');
$this.toggleClass('active')

// Pass clicked link element to another function
myfunction($this, $alias)
})

function myfunction($this, $alias) {
console.log($this.text()); // Will log Paris | France | etc...

console.log($alias); // Will output whatever is in data-alias=
}


Alias would be captures as such:



<a data-alias=Some Alias Here>Link<a>

[#63850] Thursday, December 31, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neildrews

Total Points: 166
Total Questions: 103
Total Answers: 85

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
neildrews questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Tue, Oct 12, 21, 00:00, 3 Years ago
Tue, Mar 23, 21, 00:00, 3 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
;