Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7415449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:18:19+00:00 2026-05-29T07:18:19+00:00

Im new in this JavaScript world, but i have this problem i want to

  • 0

Im new in this JavaScript world, but i have this problem i want to solve.
I need to write a generic function in javaScript, so when someone fill the value of an inputText the attribute of the chekbox next to it change to ‘inline’, and if the value is deleted the attribute of the checkbox change to ‘none’.

Im using Primfaces 3, here is my code.

<p:inputText id="inpValRezV" value="#{daniosParcialesBean.daniosParcialesDto.valorRezagoV}"
                    onchange="hideCheckFromInput(this.value, 'chkValRezV');"/>  
<p:selectBooleanCheckbox widgetVar="chkValRezV" id="chkValRezV" 
                    value="#{daniosParcialesBean.daniosParcialesDto.mcaRezagoV}" />

And my javaScript code is:

   function hideCheckFromInput(valueInput, idCheckBox){
     var chkBox = document.getElementById(idCheckBox);
     if(valueInput == ''){
        chkBox.style.display = 'none';
     }else{
        chkBox.style.display = 'inline';
    }
  }

The last fragment of code doesn’t works because getElementById is returning null!, however the next fragment works fine!

    function hideCheckFromInput(valueInput, idCheckBox){
     var chkBox = document.getElementById('principalDaniosTotales:documentacion:frmNegociacion:chkValRezV')
     if(valueInput == ''){
        chkBox.style.display = 'none';
     }else{
        chkBox.style.display = 'inline';
    }
  }

The problem is that i don’t want to hardcode the full route of the checkbox, i’m trying to write a generic function to save lines of codes and reduce de javascript code.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:18:20+00:00Added an answer on May 29, 2026 at 7:18 am

    You could take advantage of the fact that the both elements have the same client ID prefix.

    <p:inputText id="inpValRezV" onchange="hideCheckFromInput(this, 'chkValRezV')" />
    <p:selectBooleanCheckbox id="chkValRezV" />
    

    with

    function hideCheckFromInput(input, checkboxId) {
        var checkbox = document.getElementById(input.id.substring(0, input.id.lastIndexOf(":") + 1) + checkboxId);
        checkbox.style.display = (input.value) ? "inline" : "none";
    }
    

    You could alternatively also just take advantage of JSF2/PrimeFaces ajax powers so that you don’t need any line of JS code:

    <p:inputText value="#{daniosParcialesBean.daniosParcialesDto.valorRezagoV}">
        <p:ajax update="checkboxWrapper" />
    </p:inputText>
    <h:panelGroup id="checkboxWrapper">
        <p:selectBooleanCheckbox rendered="#{not empty daniosParcialesBean.daniosParcialesDto.valorRezagoV}" />
    </h:panelGroup>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used Javascript onlaod like this: function check() { var pic = new
I'm quite new to javascript programming and got stuck in this problem: I have
New to javascript, but I'm sure this is easy. Unfortunately, most of the google
I have a view that contains a javascript function that i need to init/call
I already started this in javascript so I don't want to use jquery but
I have a big problem and no idea how to solve it. I need
I'am new to jquery and maybe this is a stupid question but I have
I'm new to the wold of javascript/jquery, but had this fantastic idea for an
I have the following form and javascript function on my web page. This is
Yo. I'm really quite new to this whole JavaScript business, not to mention AJAX,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.