I'm trying to find a solution using variables or some other jQuery technique that will allow me to completely shorten the following code.
For example, how would I be able to set "ptn_" as a variable/constant and let jquery pick the number?
Or, could I just use a couple of simple variables to make things tidier and easier in the long run?
Say if I ever wanted to add more items, would I simply just be able to add the HTML code, and let the jQuery code pull the image from the CSS and display it upon click of a specific ID?
I'm a bit confused how to go about this, yes, Google is your friend and no, I haven't found a real solution anywhere :P I'm hoping someone can shed some light on how to change this. I don't expect the work doing for me, just some form of solution.
The messy jQuery:
.four
Is the container
.headercolor
Is the header color class within the container.
.patternwrap
Is the body wrapped div that will display the pattern when #ptn_X ID is clicked.
This is the function that will change the entire container holding the divs.
$(function(){
$('.button-preview').on('click', function() {
$('.mainbody').css('background', 'rgba(0,0,0,0.4)');
$('.four, .headercolor').css('color', '#fff');
})
The following are the patterns.
$('#ptn_1').on('click', function() {
$('.patternwrap').css('background-image', 'url(images/img/ptn_00001.png)');
})
$('#ptn_2').on('click', function() {
$('.patternwrap').css('background-image', 'url(images/img/ptn_00002.png)');
})
$('#ptn_3').on('click', function() {
$('.patternwrap').css('background-image', 'url(images/img/ptn_00003.png)');
})
$('#ptn_4').on('click', function() {
$('.patternwrap').css('background-image', 'url(images/img/ptn_00004.png)');
})
});
The HTML Button that when clicked, will display the jquery selector based on the ID:
<div class="button-preview" id="ptn_1">Preview</div>
<!-- Div container displaying the pattern PREVIEW -->
<div class="pattern" id="ptn_00001"></div>
The CSS:
#ptn_00001 {background:url(../images/cfpatterns/ptn_00001.png)}
#ptn_00002 {background:url(../images/cfpatterns/ptn_00002.png)}
#ptn_00003 {background:url(../images/cfpatterns/ptn_00003.png)}
#ptn_00004 {background:url(../images/cfpatterns/ptn_00004.png)}
Many thanks.