Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Set next element value based on clicked element value using jquery (or javascript)

I have two input, first is checkbox and the other is input hidden. I also have multiple input on 1 page

<input type="checkbox" class="comp-checkk" value="45164c0b-fc92-4f93-9c62-ea61336130eb">
<input type="hidden" class="comp-id" name="component_id[]" value="45164c0b-fc92-4f93-9c62-ea61336130eb"> 

<input type="checkbox" class="comp-checkk" value="45164c0b-fc92-4f93-9c62-ea61336130eb">
<input type="hidden" class="comp-id" name="component_id[]">

<input type="checkbox" class="comp-checkk" value="45164c0b-fc92-4f93-9c62-ea61336130eb">
<input type="hidden" class="comp-id" name="component_id[]">

I want to set the value of input with comp-id class using value from comp-checkk class value. And if the value of comp-id is not null, I want to set comp-id to null, else if the value of comp-id is null, I want to set comp-id value with value from comp-check

I've tried like this:

$(document).on('change','.comp-checkk',function(){
    if ($(this).next().val() != null) {
       $(this).next().val(null);
    } else {
       $(this).next().val($(this).val());
    }
});

It's working if the comp-id is not null, but when the value is null it's not working (nothing happened). I don't know what is wrong with my code.

I also made JsFiddle here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to check for both not null and also for not empty

if ($(this).next().val() != null && $(this).next().val() != '') {
   $(this).next().val(null);
} else {
   $(this).next().val($(this).val());
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!