0

I am trying to do an if/else in jquery using an custom attribute. i named the attribute "data-id" because i was hoping i can use the .data() thing of jquery but somehow it did not work.

Here is what i tried last:

 if($("#marke").val() == '0'){
    $("#modellselect").attr("data-id").not('0').hide();
 } else{
    $("#modellselect").attr("data-id").show();
 } 

So, if the marke-value is 0 i want to show only the moddellselect with data-id 0, and if the marke-value is not 0, i want to show all of the modellselect's.

2
  • Where are you executing that bit? Commented Mar 11, 2016 at 15:41
  • It also seems like you have multiple elements with same id modellselect Commented Mar 11, 2016 at 15:46

1 Answer 1

3

You need to use .filter for this instead

if($("#marke").val() == '0'){
    $("#modellselect").filter(function(){
        return $(this).data('id') != '0'
    }).hide()
 } else{
    $("#modellselect[data-id]").show();
 } 

(Remember, Id's should be unique - looks like yours are not)

Sign up to request clarification or add additional context in comments.

1 Comment

Should be able to use $("#modellselect[data-id!='0']") or $(".modellselect[data-id!='0']") selector ?

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.