views:

40

answers:

1

I am trying to set the default value for a form field in SharePoint and am having a bit of trouble getting the code to work in IE. I have tested Firefox and Chrome successfully. Any ideas why IE would not be setting the value?

<script type="text/javascript" src="http://www.qg.com/shared/cache/jquery/142/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
    setTimeout("setDefaultText()", 500);    
    function setDefaultText()
    {   
        var text = "Param 1:\n\nParam 2:\n\nParam 3:";

        var r1text = $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl09_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val();
        var r2text = $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl10_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val();

        if((r1text + "").length == 0)
        {
            $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl09_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val(text);
        }

        if((r2text + "").length == 0)
        {
            $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl10_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val(text);
        }
    }
</script>
+1  A: 

What is the purpose of the setTimeout()?

If you're trying to make sure the DOM is loaded, then you should do this:

$(function() {   
        var text = "Param 1:\n\nParam 2:\n\nParam 3:";

        var r1text = $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl09_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val();
        var r2text = $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl10_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val();

        if((r1text + "").length == 0)
        {
            $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl09_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val(text);
        }

        if((r2text + "").length == 0)
        {
            $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl10_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val(text);
        }
});

I'm guessing the DOM wasn't loaded in 500 milliseconds, and your .val() was giving you undefined, so with the + "" you were ending up with a string "undefined".

patrick dw
I started with the jQuery style of waiting for the DOM but was having issues related to a separate syntax error that I had attributed to SharePoint conflicting with the jQuery DOM load check. Interestingly switching back to the jQuery style fixed this issue but I don't understand why that would be.
jjr2527
@jjr2527 - That is strange. There really shouldn't be a conflict with jQuery's `.ready()` method since it is just checking to see if the document has a `<body>` tag every `13ms` or so (until it finds one). Anyway, glad it cleared up for you. :o)
patrick dw