0

I try desperately for hours and hours to get this 2 little script working without success . If I use them just like that, no one work but if I include only one no problem. So the issue is not with the code itself but with a conflict when code are used together. I tried dozen of combination and also by adding some else statement but without luck too.


CODE 1

<script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
if (event=='reserve')
var mobile_number_param = document.getElementById('mobile_number_param');
mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
mobile_number_param.readOnly = true;
var email = document.getElementById('email');
email.readOnly = true;
var first = document.getElementById('first');
first.readOnly = true;
var last = document.getElementById('last');
last.readOnly = true;
}
// ]]></script>

CODE 2

<script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
if (event=='show_cart')
document.getElementById('empty_cart_block').style.display='none';
}
// ]]></script>

Unfortunately I'm not a coder...Someone have a suggestion please ?

8
  • 2
    Hard to tell whats going on with no indentation, no brackets for if statements... Commented Apr 28, 2013 at 21:50
  • Did you try debugging it ? Commented Apr 28, 2013 at 21:51
  • I think the first one is incorrectly written and should have everything after the if line enclosed in another set of curly braces. Then move lines 3 and 4 of the second block between the now two closing curly braces of the first block. Commented Apr 28, 2013 at 21:52
  • 1
    Got edited out by someone else than the OP - no idea why someone would do that. Commented Apr 28, 2013 at 21:57
  • 1
    @GCATNM: Oh, wow, I see it. User 'Stedy' removed it. That is not cool, I've put it back. Commented Apr 28, 2013 at 21:59

4 Answers 4

1

You can move them both into the same function. on_my_form_loaded and then use an if else to determine which chunk of code should execute. Like:

    <script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
    if (event=='show_cart')
    {
        document.getElementById('empty_cart_block').style.display='none';
    }
    else if (event=='reserve')
    {
        var mobile_number_param = document.getElementById('mobile_number_param');
        mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
        mobile_number_param.readOnly = true;
        var email = document.getElementById('email');
        email.readOnly = true;
        var first = document.getElementById('first');
        first.readOnly = true;
        var last = document.getElementById('last');
        last.readOnly = true;
    }
}

// ]]></script>

If the event is show_cart then the first chuck of code will run. If the event is reverse then the second chunk of code will run.

Sign up to request clarification or add additional context in comments.

1 Comment

work great...many thank! In fact I think I tried this code but I was unable to get working. I had probably forget some brackets...
1

Try something like this:

<script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
    if (event=='reserve') {
        var mobile_number_param = document.getElementById('mobile_number_param');
        mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
        mobile_number_param.readOnly = true;
        var email = document.getElementById('email');
        email.readOnly = true;
        var first = document.getElementById('first');
        first.readOnly = true;
        var last = document.getElementById('last');
        last.readOnly = true;
    }
    else if (event=='show_cart') {
        document.getElementById('empty_cart_block').style.display='none';
    }
}
// ]]></script>

Comments

1
<script type="text/javascript">// <![CDATA[
function on_my_form_loaded(event) {
    if (event=='reserve') {
        var mobile_number_param = document.getElementById('mobile_number_param');
        mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
        mobile_number_param.readOnly = true;
        var email = document.getElementById('email');
        email.readOnly = true;
        var first = document.getElementById('first');
        first.readOnly = true;
        var last = document.getElementById('last');
        last.readOnly = true;
    }

    else if (event=='show_cart') {
        document.getElementById('empty_cart_block').style.display='none';
    }
}
// ]]></script>

Did you want it?

Comments

0

There are better ways to handle this, but try using a switch statement:

function on_my_form_loaded(event) {
    switch (event) {
    case "reserve":
        var mobile_number_param = document.getElementById('mobile_number_param');
        mobile_number_param.value = "[cb:userdata field="cb_myfield" user="#me" /]";
        mobile_number_param.readOnly = true;
        var email = document.getElementById('email');
        email.readOnly = true;
        var first = document.getElementById('first');
        first.readOnly = true;
        var last = document.getElementById('last');
        last.readOnly = true;
        break;
    case "show_cart":
        document.getElementById('empty_cart_block').style.display = 'none';
        break;
    }
}

1 Comment

To whoever downvotes the best answer so far while staying incognito - if there is indeed something wrong with it (which I don't see), giving your reasons for doing so could help improve it and make it more useful than you think it is right now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.