0

I have a form that selects an image for the customer to customize, from there it should show a set of options that are customizable for that particular product, e.g colours, trims etc

First you choose your product and then I want to show certain customization options once that is selected.

Not all products can be customized in the same way so I need to be able to remove those options that are not applicable. I can't use the value because thats already used up to change the image shown for the Choose your Product option

link

I have set the name of each option to a numeric value, can I do this

Is this the correct way to start it?

if ($(this).name() == "1,2,3,4") 

Then do some stuff

Any help on this would be appreciated, I havent done this for a while and my brain is drawing a blank.

zoranc
  • 2,364
  • 1
  • 17
  • 34
Denise Field
  • 141
  • 1
  • 1
  • 9
  • Do you want to know if `$(this).name()` is equal to any of those numbers? or do you want to do something different for each number – Matthew Shine Mar 10 '14 at 10:36

4 Answers4

0

You can use the attribute selector:

var elementOne = $('[name="1"]');

More info on Attribute selectors and jQuery Attribute Selectors.

Ex-iT
  • 1,438
  • 2
  • 12
  • 18
0

There is no method called .name(), but in this case that doesn't look like what you want to do. You can't use a comma-separated string as an array of values. What you want to do is something like this:

<select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="4">4</option>
</select>

$('select').on('change', function(){
    if ($.inArray($(this).val(), ['1', '2', '3']) > -1)
    {
        alert('in array');
    }
});

http://jsfiddle.net/Yb2t9/

Gavin
  • 5,796
  • 2
  • 44
  • 67
0

You can use following

alert($(this).attr('name'));

and check if its in array list using inArray

Pratik
  • 10,715
  • 5
  • 33
  • 69
0

Could you not just use if else statements if you wish to do something different for each number?

var pNo = $(this).name();

if (pNo == 1)
{
   //Do something...
}
else if (pNo == 2)
{
   //Do something...
}
else if (pNo == 3)
{
   //Do something...
}
else if (pNo == 4)
{
   //Do something...
}
Matthew Shine
  • 453
  • 4
  • 16
  • A [switch statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) would be more appropriate in your example I think. – Ex-iT Mar 10 '14 at 10:40
  • Performance (and readability imho). See [Writing Efficient JavaScript: Chapter 7 - Even Faster Websites](http://oreilly.com/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html#fast_conditionals) and on [Javascript switch vs. if…else if…else](http://stackoverflow.com/q/2922948/3351720) – Ex-iT Mar 10 '14 at 11:02