1

I'm trying to put together a small interface builder, where based on the select field values, specific classes are added or removed.

I'm want to write the jquery so that it can refer to any select field and just remove/add the classes from that select box, without needing to delcare the select ids in the jquery

Currently I have three a select boxes for textcolour, backgroundcolour, arrowdirection

Here's my script so far

$('select').change(function()
{
    var thisname = $(this).attr('name'),
        thisselect = $(this).val();


    $('#arrowlink').removeClass(thisname).val();
    $("#arrowlink").addClass(thisselect).val();

});

This where I am so far http://cdpn.io/nvGuH

Thanks

4
  • You're using .val() as getter not as setter. Wrong. May we see some HTML please? Commented Apr 22, 2013 at 21:44
  • What are you trying to get from name and what you have inside val please show something Commented Apr 22, 2013 at 21:59
  • Here's my codepen cdpn.io/nvGuH Commented Apr 22, 2013 at 22:06
  • Here's an not totally functional example (I have no time to fix everything) but you can play with jsbin.com/efeqal/1/edit it's using the logic from my answer. Hope will be helpful. Commented Apr 22, 2013 at 22:38

2 Answers 2

2

I'll show you 2 solutions:

  • one using your logic with an existent CSS
  • One without CSS (! yes, without !)

Let's say you have a select name and options values like:

  <select name="bg">
    <option value="">Background</option>
    <option value="red">red</option>
    <option value="green">green</option>
    <option value="blue">blue</option>    
  </select>

  <select name="color">
    <option value="">Color</option>
    <option value="red">red</option>
    <option value="green">green</option>
    <option value="blue">blue</option>    
  </select>

We will retrieve the selects names and values (if there's one) and combine them inside an array like (example after selections):

["bg-red", "color-blue"]

now to set that array as class that means you have to prepare all those classes inside your CSS:

.bg-red{  
  background:red; 
}
.bg-blue{  
  background:blue; 
}
.bg-green{  
  background:green; 
}

.color-red{  
  color:red; 
}
.color-blue{  
  color:blue; 
}
.color-green{  
  color:green; 
}

Than we can use .attr('class', classesArr.join(' ')) to rebuild all the element's desired classes.

LIVE DEMO

function createClasses( element ){

  var classesArr = []; // array to store all the classes

  $('select').each(function(){
     var myVal  = this.value;  // get the value // blue, red, green
     if(myVal.length)          // it there's a value join the value with the select name, ex: bg-blue, bg-red .... or color-blue, color-red ....
        classesArr.push( this.name +'-'+ this.value );
  });

  console.log(classesArr);  // open console to see what happens!
  $(element).attr('class', classesArr.join(' ') ); // rebuild all the new classes

}



$("select").on("change", function(){
    createClasses( "#arrowlink" );    // run function and pass as argument the desired element to play with
});

Edit: simplest solution!

As I don't know why you bother with all those classes I suggest you to remove headaches creating all those classes by using this simple trick:

LIVE DEMO

CSS:

NO CSS!!!!!

HTML:

  <div id="arrowlink">ARROW LINK</div>

  Please set:

  <select name="background-color">
    <option value="">Background</option>
    <option value="red">red</option>
    <option value="green">green</option>
    <option value="blue">blue</option>    
  </select>

  <select name="color">
    <option value="">Font Color</option>
    <option value="red">red</option>
    <option value="green">green</option>
    <option value="blue">blue</option>    
  </select>

  <select name="font-size">
    <option value="">Size</option>
    <option value="9">9</option>
    <option value="12">12</option>
    <option value="18">18</option>
    <option value="24">24</option>  
  </select>

create an object for the jQuery's .css() method exaple:

function createClasses( element ){
  var classesArr = {};
  $('select').each(function(){
     var myVal  = this.value ;
     if(myVal.length) classesArr[this.name] = this.value;
  });
  console.log(classesArr);  
  $(element).css(classesArr);
}

$("select").on("change", function(){
    createClasses( "#arrowlink" );
});
Sign up to request clarification or add additional context in comments.

3 Comments

+1 removed my answer, he posted what he was actually trying to do after I wrote it.
@roXon Thank you so much, option one worked perfectly, I just modified classesArr.push( this.name +'-'+ this.value ); to this classesArr.push( this.value );
@onebitrocket I'm glad you took the time to figure out my code! good job! And happy coding!
0

If you're just looking to set the class to be equal to the value of the selection, try this:

$("select").on("change", function(){
    var val = this.value;
    $("#arrowlink").attr("class", val);
});

6 Comments

Don't use $ for values it's not the way it's meant to be used. Just for variables that represent an Element, and instead of $(this).val(); know your JS: this.value will return the same. Than you're missing # instead of $ and ); at the function end.
Another suggestion would be to clear any class added before a new edit, which you forgot to mention.
@roXon I understand that would return the same thing. However, that is how I write my JavaScript.
Kacey, here is a demo with your code before my edit: jsbin.com/iteqib/5/edit
@roXon Thanks for the updates to it, but I typed it out really quick as an answer. In my experience, the OP is supposed to learn a little something anyway, isn't he? :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.