I am trying to set var for my slideshow using a class .selected, but it doesn't work. I am assuming that I have a problem with syntax, but funny enough, I have spent about 2 hours trying to find the correct way of doing it, and didn't find anything relevant! So, is it even possible (and if it is, how do I do it?) to have something like:
var name = $('.div').attr('title').split('')[3]
My particular code is: var currentIndex = $(.room_exp .selected).attr('title').split('')[3]
is it supposed to work?
Basically, I have a list of thumbnails (sort of), upon clicking which, I require a more detailed version to open at the correct slide. I believe I can access the number in the title of the thumbnail and use .split. Please help!)
CODE BELOW:
<li class="room_c"><div class="room_list" data-room="one"><img src="site-assets/photos/rooms1.jpg"><div class="room-description"><span class="label_bold">Эконом</span><br><span class="desc"> Две кровати</span></div><div class="room_box"><a href="/photos"><span class="camera_left"><img src="site-assets/camera_icon.png" style="height: 16px; width: auto; position: relative;"></span></a><span class="camera_right"><div class="room_mdts" data-room="one" title="Номер класса Эконом">Дополнительная информация</div></span></div></div></li>
<li class="room_c"><div class="room_list" data-room="two"><img src="site-assets/photos/rooms2.jpg"><div class="room-description"><span class="label_bold">Стандарт</span><br><span class="desc">Двойная кровать</span></div><div class="room_box"><a href="/photos"><span class="camera_left"><img src="site-assets/camera_icon.png" style="height: 16px; width: auto; position: relative;"></span></a><span class="camera_right"><div class="room_mdts" data-room="two" title="Номер класса Стандарт">Дополнительная информация</div></span></div></div></li>
etc.
<div id="room_details_holder">
<div id="room_name_and_close"><button class="room_details_close">X</button> </div>
<div class="room_details">
<div id="rooms_nav_bar"><button class="room_previous"><<</button>
<div class="room_name"><div id="room_name"></div></div>
<button class="room_next">>></button></div>
<div class="room_exp" title="Room Type 1" data-room="one" data-number="1">
<div class="room_split_l"><div id="room1_photo"><img src="site-assets/photos/rooms1.jpg"></div> <div class="book"></div> </div>
<div class="room_split_r"><div class="room_description"> </div>
</div>
</div>
<div class="room_exp" title="Room Type 2" data-room="two" data-number="2">
<div class="room_split_l"><div id="room2_photo"><img src="site-assets/photos/rooms2.jpg"></div> <div class="book"></div> </div>
<div class="room_split_r"><div class="room_description"> </div>
</div>
etc.
and the javascript I'm using: $(".room_mdts").click(function(event){ //get the target var target = event.currentTarget; var room = $(target).data("room");
//First way, by reuse target (only inside this function)
$(target).addClass(room);
//The second, by using selectors
//remove all "selected" classes to all which have both "room" and "selected" classes
$('.room_exp.selected').removeClass("selected");
//add "selected" class to the current room (the selector can also be the target variable)
$('.room_exp[data-room='+room+']').addClass("selected");
});
var currentIndex = 0,
items = $('.room_details .room_exp'),
itemAmt = items.length;
function cycleItems() {
var item = $('.room_details .room_exp').eq(currentIndex);
items.hide();
item.css('display','inline-block');
}
$('.room_next').click(function() {
currentIndex += 1;
if (currentIndex > itemAmt - 1) {
currentIndex = 0;
}
cycleItems();
});
$('.room_previous').click(function() {
currentIndex -= 1;
if (currentIndex < 0) {
currentIndex = itemAmt - 1;
}
cycleItems();
$('#room_name').text($('.room_exp:nth-child('+ (currentIndex+1)+')').attr('title'));
});
$('.room_next').click(function(){
$('#room_name').text($('.room_exp:nth-child('+(currentIndex+1)+')').attr('title'));
});
$('.room_previous').click(function(){
$('#room_name').text($('.room_exp:nth-child('+(currentIndex-1)+')').attr('title'));
});
$('#room_name').text($('[style*="inline-block"].room_exp').attr('title'));
});
So, upon clicking the room_list, a corresponding room_exp opens, with side navigation buttons next and previous. The problem is that a)the title doesn't show up upon choosing a room, only after next or previous is clicked and b) the currentIndex set at 0, so after next or previous is hit - the slide goes to either the second or the last one, respectively (so, say I opened 6th expanded slide and pressed next, it goes to 2nd instead of 7th.) Hope, it's clear now)) Any ideas?
Here's a fiddle: https://jsfiddle.net/ekh20dxz/2/