I’m having a conflict with a jQuery feature im adding to a wordpress backend. There will be the option of adding two of these features to a page, but when there is two, the first one works and the second one doesnt. They both HAVE to share the same classes and ID’s
Here’s my fiddle: http://jsfiddle.net/yQMvh/39/
HTML
<div class="plugin">
<div class="iconDisplay">Display's selected icon</div>
<span id="selectedIcon" class="selected-icon"></span>
<button id="selectIconButton">Select Icon</button>
<div id="iconSelector" class="icon-list">
<div id="iconSearch">
<label for="icon-search">Search Icon: </label>
<input type="text" name="icon-search" value="">
</div>
<span class="icon-orange"></span>
<span class="icon-teal"></span>
<span class="icon-icon3"></span>
<span class="icon-icon4"></span>
<span class="icon-icon5"></span>
<span class="icon-icon6"></span>
<span class="icon-icon7"></span>
<span class="icon-tealer"></span>
</div>
</div>
<div style="height: 50px"></div>
<div class="plugin">
<div class="iconDisplay">Display's selected icon</div>
<span id="selectedIcon" class="selected-icon"></span>
<button id="selectIconButton">Select Icon</button>
<div id="iconSelector" class="icon-list">
<div id="iconSearch">
<label for="icon-search">Search Icon: </label>
<input type="text" name="icon-search" value="">
</div>
<span class="icon-orange"></span>
<span class="icon-teal"></span>
<span class="icon-icon3"></span>
<span class="icon-icon4"></span>
<span class="icon-icon5"></span>
<span class="icon-icon6"></span>
<span class="icon-icon7"></span>
<span class="icon-tealer"></span>
</div>
</div>
jQuery
var iconVal = $(".icon_field").val();
$('#selectedIcon').addClass(iconVal);
$("#selectIconButton").click(function () {
$("#iconSelector").fadeToggle();
});
$("#iconSelector span").click(function () {
selectIcon($(this));
});
function selectIcon(e) {
var selection = e.attr('class');
$(".icon_field").val(selection);
$("#iconSelector").hide();
$('#selectedIcon').removeClass();
$('#selectedIcon').addClass(selection).show();
return;
}
$('input[name="icon-search"]').keyup(function(){
var sValue = $(this).val().toLowerCase();
$.each($('span'), function(){
if($(this).attr('class').indexOf(sValue)===-1){
$(this).fadeOut(0);
}else{
$(this).fadeIn(0);
}
});
});
The issue is pointed out clearly in the comments.
ID
must be unique on any given page.id="iconSelector"
– just add this as a class along withicon-list
so it will look likeclass="iconSelector icon-list"
. By using standard jQuery selector techniques you should still be able to interact with the DOM as needed.This requirement is illogical. You’ll have to rework your process.
A little advice to help you along the way – you can utilize
$(this)
to know which DOM element has triggered an event – so you don’t need to worry aboutID
.It’s not super fancy, but is this the functionality you are looking for?
http://jsfiddle.net/ccarterc1984/RfW69/
CSS:
HTML:
jQuery:
Use the classes or element types rather than the id’s. Add classes if necessary. For example,
becomes
—
becomes
As mentioned in the comments you cannot use multiple ids.
Full example:
HTML
JQUERY