The javascript to display the button does not work when filtering

Markule 1 Reputation point
2022-09-21T15:48:27.77+00:00

Hi,

I have javascript on a Sharepoint 2013 page that displays an html button.
It is working when the page is loaded. In the column is a button.
When the user filters, the script does not work and the button is displayed as html.

Javascript:

<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$('td.ms-vb2 a').each(function(){
var linkUrl = $(this).attr('href');
var text = $(this).text();
$(this).hide();
$(this).parent('td').append('<input type="button" value="' + text +'" onclick="Callbutton(\'' + linkUrl +'\')"></input>');
});
function Callbutton(url){
window.location.href=url;
}
</script>

In the cell is this:

<button onclick='Javascript:MyFce(ID)'>CLICK</button>

Thank you very much for any help.

Microsoft 365 and Office SharePoint Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-09-22T02:02:29.497+00:00

    Hi @Markule
    You need to add $(document).ready(function(){ }); if you want to load the function when the page is loaded. You can refer to following code

    $(document).ready(function() {  
    	$('td.ms-vb2 a').each(function() {  
    		var linkUrl = $(this).attr('href');  
    		var text = $(this).text();  
    		$(this).hide();  
    		$(this).parent('td').append('<input type="button" value="' + text +  
    			'" onclick="Callbutton(\'' + linkUrl + '\')"></input>');  
    	});  
      
    	function Callbutton(url) {  
    		window.location.href = url;  
    	}  
    });  
    

    For more details
    https://www.w3schools.com/jquery/event_ready.asp


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. Ganesh Sanap 211 Reputation points
    2022-09-22T06:32:14.5+00:00

    You can run function after everything loads on SharePoint like:

    function runAfterEverythingElse(){  
        // your code  
    }  
     _spBodyOnLoadFunctionNames.push("runAfterEverythingElse");  
    

    You can find other ways of doing it in my answer at: Run JavaScript after page loads

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.