Monday, May 18, 2015

Lookup column - "Add new items" hyperlink on the list's EditForm

For better user experience, sometime we need to add a new value in the lookup column. So we need to have a hyperlink just beneath the lookup column in EditForm of the list. We can achieve this by using this code snippet.

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>


<script type="text/javascript">
var targetNewFormUrl = "{url of the lookup list NewForm.aspx}";
$(document).ready(function(){
    var sourceNewFormUrl = "";
    if(document.URL.indexOf("?") > 0)
        sourceNewFormUrl = encodeURIComponent(document.URL.substring(0, document.URL.indexOf("?")));
    else
        sourceNewFormUrl = encodeURIComponent(document.URL);
    targetNewFormUrl += "?source=" + sourceNewFormUrl ;

    var h = "<br/><a href='"+targetNewFormUrl +"' >Add new item</a>";
    $($( "nobr:contains('Modality String'):last" )).parent().parent().siblings().children().after(h); //use your own column name

});
</script>

No comments: