3

I have a couple of table rows and tables with hidden input fields and values. What i need to do is loop through all the hidden input fields and find a specific field where the class = 'something' and value = something and go up to the <td> of that input field and change the background color.

 $('input:hidden').each(function(){
    if( $(this).find(".id_schedule_hours") && $(this).val() == 1) {
        console.log('here')
    }
}

4 Answers 4

6

.each() is not necessary; you can use attributes selector, .closest()

$("input:hidden[class='something'][value='something']")
.closest("td").css("backgroundColor", /* color */)
Sign up to request clarification or add additional context in comments.

13 Comments

can you tell me how to do multiple checks such as class='something' value = 'something' and class='something2' and value = 'something2'
@Yeak "how to do multiple checks such as class='something' value = 'something' and class='something2' and value = 'something2'" How can input element have more than one value? Can you include html at Question? Try $("input:hidden.something.something2[value*='something'][value*='something2']")
@Yeak Note, "how to do multiple checks such as class='something' value = 'something' and class='something2' and value = 'something2'" does not appear at original Question
No i meant check against multiple input fields so check against a hidden field with class something and value something1 and another hidden field with class something2 and value something 2
@Yeak " No i meant check against multiple input fields so check against a hidden field with class something and value something1 and another hidden field with class something2 and value something " This description appears to be beyond scope of original Question? What do you mean by "check against a hidden field"?
|
1

to add unique values to be checked against - you could use data attributes and set the desired value to check against in a data variable. Then in the function - you get the data attribute value (irrespective of the input class) annd compare the value of that desired value:

//HTML
<input type="hidden" class="id_schedule_hours" data-desiredvalue="1" name="id_schedule_hours" value="" />

<input type="hidden" class="id_trucks" data-desiredvalue="2" name="id_trucks" value="" />

//js
$('.id_schedule_hours,id_trucks').each(function(){ 
      var desiredValue=$(this).attr('data-desiredvalue');
      if( $(this).val() == desiredValue) {
            console.log('here');
            $(this).parent().css('background-color','red');
      }
 });

Comments

0

Use the class as a selector and use .parent() to go up the chain and change the parent:

 $('input[type=hidden].id_schedule_hours').each(function(){
      if( $(this).val() == 1) {
            console.log('here');
            $(this).parent().css('background-color','red');
      }
 });

Would also suggest changing your selector name - it seems wrong to have a class called .id_...

Also - if the only inputs that class are the hidden ones you are interested in, then you can just target them directly in the selector eg:

$('.id_schedule_hours').each(function(){

3 Comments

The second one worked perfect is there a way to do multiple checks at once for example id_schedule_hours and id_trucks?
you can chain the selectors with a comma between them $('.id_schedule_hours,id_trucks').each(function(){
then how would i compare each value - so id_schedule_hours value = 1 and id_trucks value = 2
0

You could leverage the jQuery :hidden selector. It is not part of the official CSS selectors, but a jQuery selector.

$('input.id_schedule_hours:hidden').each(function(){
    if( $(this).val() == 1) {
        console.log('here')
    }
}

Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout. During animations that hide an element, the element is considered to be visible until the end of the animation.

Elements that are not in a document are not considered to be visible; jQuery does not have a way to know if they will be visible when appended to a document since it depends on the applicable styles.

This selector is the opposite of the :visible selector. So, every element selected by :hidden isn't selected by :visible and vice versa.

During animations to show an element, the element is considered to be visible at the start of the animation.

How :hidden is determined was changed in jQuery 1.3.2. An element is assumed to be hidden if it or any of its parents consumes no space in the document. CSS visibility isn't taken into account (therefore $( elem ).css( "visibility", "hidden" ).is( ":hidden" ) == false). The release notes outline the changes in more detail.

jQuery 3 slightly modifies the meaning of :hidden (and therefore of :visible). Starting with this version, elements will be considered :hidden if they don't have any layout boxes. For example, br elements and inline elements with no content will not be selected by the :hidden selector.

See: jQuery :hidden documentation

Comments

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.