Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

JavaScript in the DOM popup


Go to End


4 Posts   1854 Views

Avatar
Stef87

Community Member, 66 Posts

5 October 2012 at 3:19am

Edited: 15/10/2012 11:22pm

Hi

I am using 2.4.7 and want to add some javascript to the DOM popup. Basically what I want to happen is that when a certain value in a dropdown box is selected, another field appears (a textfield in this case).

I tried this tutorial but had absolutely no success.

I would be over the moon if someone could give me a hint about it. It is driving me crazy.

Thanks

Edit

My attempts have been as unsuccessful as when I posted this but here is my code anyway.

(function($) {
    $(document).ready(function(){
        var dropDown = $('#DataObjectManager_Popup_DetailForm_Status');
        var reason = $('#DataObjectManager_Popup_DetailForm_Reason');
       
        if(dropDown.val() == 'rejected' || 'Rejected'){
            reason.show();
        }
        else{
            reason.hide();
        };
    
	    dropDown.change(function (e) {
	        if(dropDown.val() == 'rejected' || 'Rejected'){	
	        	reason.show();
                alert(dropDown.val())
	        }
	        else{
	        	reason.hide();
	        }
       })
    });
})(jQuery);

The reason textfield disappears as it is supposed to when I hit save but not when the dropdown is changed.

Avatar
Stef87

Community Member, 66 Posts

15 October 2012 at 11:22pm

11 days and 143 views later nobody has any advice?

From my investigations it turns out that the Javascript is being called ok but the change part is not being applied. I'm stumped as to why not?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

16 October 2012 at 3:50pm

Check your script.

if(dropDown.val() == 'rejected' || 'Rejected'){
reason.show();
}

^^ This always returns true, Because "Rejected" is a string with > 0 length, it is always evaluated as true.

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

Avatar
Stef87

Community Member, 66 Posts

16 October 2012 at 9:00pm

Thank you so much. I can't believe I was stuck on something so simple.