You are viewing limited content. For full access, please sign in.

Question

Question

Javascript help

asked on October 16, 2024 Show version history

Hello,

I have an issue with javascript, I want to hide the Submit button which I have renamed as "Save"

 

The code works fine on the forms without the CSS, however due to pagination I have following CSS to show Save (Submit button) on all pages:

/* Show the Submit button on all pages */
.Submit {
  display: initial !important;
}

 

The following JS works fine when above CSS is disabled:

$(document).ready(function() {

//Call to function for hiding submit button when ReadOnly user log's on
    checkDisplayNameValue(); 
  
  
  // Get the active page
  $('.cf-pagination-tabs').click(function() {

    $('.active-page input').val($('.cf-pagination-tabs li.active').index());
  });

// Find the Read input fields, this is a hidden field where the value is DisplayName
    var fieldValue = $('#Field492').find('#Field492 input');
    console.log("DisplayName fields found: ", fieldValue.length);
  
  
  	//var fieldValue = $('.Read').val(); // Get the value of Field40
    var fieldValue = $('#Field492').val(); // Get the value of Field40
    toggleSubmitButton(fieldValue); // Call the function to show/hide the submit button based on the value
}


// Function to toggle the Submit button based on the field value
function toggleSubmitButton(fieldValue) {
    var $submit = $('.Submit'); // Find the submit button
    if (fieldValue === "ReadOnly") {
        $submit.hide();  // Hide the submit button if value is "ReadOnly"
        console.log("Submit button hidden because Field40 is ReadOnly.");
    } else {
        $submit.show();  // Show the submit button if value is anything else
        console.log("Submit button shown.");
    }
}

Please help as this is really stopping me from getting the solution working...

 

Many thanks in advance,

S

0 0

Answer

SELECTED ANSWER
replied on October 16, 2024

I still think the issue has to be around the show() and hide() functions and the CSS interacting in a weird way.  Ultimately, what is happening when you use the show() and hide() functions in JQuery is that it is manipulating the display setting of the element.  You have some CSS that changes the display setting and then you have JQuery functions that are too, and how the form is behalving in an unexpected manner.

How about, instead of using show() and hide() on the Submit button, you disabled it and showed it kind of greyed out?  I've done this on a bunch of forms.

Instead of:     $submit.hide();
Try:     $('.Submit').prop('disabled', true).css('opacity', '.4');

Instead of:     $submit.show();
Try:     $('.Submit').prop('disabled', false).css('opacity', '1');

That way, you can keep the CSS without the script impacting the same thing.

0 0

Replies

replied on October 17, 2024

Matt has part of the answer correct, because you are using the !important classifier in your CSS the show() and hide() functions are modifying the inline styles of the submit button which are then being overridden by the !important flag in your css. You shouldn't need that flag to make your css work

1 0
replied on October 17, 2024

That's what I get for not actually trying to test it before I responded.  haha

0 0
replied on October 16, 2024 Show version history

I haven't tested this - but what happens if - instead of using the CSS and the Javascript - you tweak your Javascript so that instead of showing and hiding the submit button, it applies the CSS when you want it shown?

Instead of:     $submit.hide();
Use:     $submit.css('display', 'none');

Instead of:     $submit.show();
Use:     $submit.css('display', 'initial');

 

Side note - the Modern Designer can hide and show the submit buttons via Field Rules - if probably couldn't do it based on a specific field being readonly - but you could probably work out an appropriate field value to trigger the field rule instead.

**EDITED to remove !important from the suggestions for the css functions.**

0 0
replied on October 16, 2024 Show version history

I tried, but doesn't work....

 

I am using the classic version as there's lot of work done.... 

 

ReadOnly is a value passed in a hidden field, it works fine when the CSS for showing Submit on every page is removed.

 

Please note that the Label for Submit is Save

 

Thanks!

S

0 0
SELECTED ANSWER
replied on October 16, 2024

I still think the issue has to be around the show() and hide() functions and the CSS interacting in a weird way.  Ultimately, what is happening when you use the show() and hide() functions in JQuery is that it is manipulating the display setting of the element.  You have some CSS that changes the display setting and then you have JQuery functions that are too, and how the form is behalving in an unexpected manner.

How about, instead of using show() and hide() on the Submit button, you disabled it and showed it kind of greyed out?  I've done this on a bunch of forms.

Instead of:     $submit.hide();
Try:     $('.Submit').prop('disabled', true).css('opacity', '.4');

Instead of:     $submit.show();
Try:     $('.Submit').prop('disabled', false).css('opacity', '1');

That way, you can keep the CSS without the script impacting the same thing.

0 0
replied on October 16, 2024

The requirements are that the user for whom ReadOnly comes should not be able to submit form.

 

I'm guessing this way they can by mistake submit?

0 0
replied on October 16, 2024 Show version history

Just like hiding the button should prevent submission disabling it should as well.  Of course, a user could technically go into the browser console and unhide it or remove the disabled status - but why would they be that insistent on forcing it?

Either option is about the same level of restriction.

0 0
replied on October 16, 2024

Thanks Mathew!

 

I actually wrote without testing, however this is Brilliant! 

Many Thanks.

0 0
replied on October 16, 2024

Thanks Mathew,
The issue is submit should only Hofer for some users & for others should be visible on all tabs....

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.