Reputation: 583
This statement will look for the div #content-company
and load the appropriate data when it finds it. However my problem is:
On this occasion I have to use a slider that I can't rename to the same as the div so in this instance I have an additional called .slides
I normally would do this:
// JavaScript Document
$(function(){
$('.tileSB').click(function(e){
e.preventDefault();
var url = $(this).attr('href') + ' #' + $(this).attr('data-target');
$('#content-company, .slides').load(url);
});
});
But that simply overlays both the content associated with the div and the slides class so you have them with the same name at the same time and I don't want that.
Can someone explain how I can have this?:
// JavaScript Document
$(function(){
$('.tileSB').click(function(e){
e.preventDefault();
var url = $(this).attr('href') + ' #' + $(this).attr('data-target');
$('#content-company OR .slides').load(url);
});
});
Thanks in advance
Upvotes: 0
Views: 52
Reputation: 272156
You can simply use this:
$("#content-company, .slides").first()
Note that first element is determined according to HTML order. If more than one matching elements are found then the first one in HTML order is returned.
Upvotes: 1
Reputation: 87203
You can use ? :
(ternary operator). The code below will use the #content-company
, if it existst otherwise uses .slides
.
var $selector = $('#content-company').length ? $('#content-company') : $('.slides');
$selector.load(url);
Upvotes: 2
Reputation: 337590
You can check if the #content-company
element exists. If it doesn't you can use .slides
instead. Something like this:
$('.tileSB').click(function(e){
e.preventDefault();
var url = $(this).attr('href') + ' #' + $(this).attr('data-target');
var $target = $('#content-company');
if (!$target.length)
$target = $('.slides');
$target.load(url);
});
Upvotes: 5