Solved

.removeAttr() not working in my SharePoint JSOM code

Posted on 2016-10-31
8
35 Views
Last Modified: 2016-11-03
I had this question after viewing Remove 'disabled' attribute.  I implemented the same code below in a list that is generated dynamically but it's not working.  

One of the fields should get disabled if user is in the SharePont 'Manager' group but nothing happens.  If you would like the creds to my SharePoint site so you can see for yourself.  Let me know and I will send you the creds to your inbox.  Thanks!


jQuery(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");  
    
 console.log("Entering SPServices");
	$().SPServices({
 		operation: "GetGroupCollectionFromUser",
 		userLoginName: $().SPServices.SPGetCurrentUser(),
 		async: false,
 		completefunc: function (xData, Status) {
 		console.log(xData.responseXML.xml);
		if ($(xData.responseXML).find("Group[Name='Manager']").length == 1) {
 			$('[id^=faRowID]').removeAttr('disabled');
 		}
 		else {
 			alert("login outside of the Group user");
 		}
	  }
 
	}); /*close().SPServices({ */        
});

	/*New to make this dynamic-had to get it working first.*/
	var htmlTbl = "<table border='0' cellspacing='5' cellpadding='5'>"+
			  "<tbody>"+
			  "<tr><td class='bold'>Minimum Cost:</td><td><input type='text' id='min' name='min'></td></tr>"+
			  "<tr><td class='bold'>Maximum Cost:</td><td><input type='text' id='max' name='max'></td></tr>"+
			  "</table>"+
	    	  "<table id='prioritize' class='display' cellspacing='0' width='100%'>"+
	          "<thead>"+
		          "<th>ID</th>"+
		          "<th>Project Title</th>"+
		          "<th>Facility Priority</th>"+
		          "<th>VISN Priority</th>"+
		          "<th>National Priority</th>"+
		          "<th>Project Number</th>"+
		          "<th>VISN</th>"+
		          "<th>Station</th>"+
		          "<th>Total Project Cost</th>"+
		          "<th>Design Fiscal Year</th>"+
		          //"<th>Construction Fiscal Year</th>"+
	          "</thead>"+
	     	  "<tbody>";


	//Retrieves all list items- Will need to modify to take in query string data to pass to CAML.
	function retrieveListItems() {
		var clientContext = new SP.ClientContext();
		var oList = clientContext.get_web().get_lists().getByTitle('Project Module');
		
		var camlQuery = new SP.CamlQuery();
	  
		this.collListItem = oList.getItems(camlQuery);
		
		clientContext.load(collListItem);
		
	    clientContext.executeQueryAsync(
	        Function.createDelegate(this, this.onQuerySucceeded),
	        Function.createDelegate(this, this.onQueryFailed)
	        );
	}

	function onQuerySucceeded(sender, args) {
	    var listItemInfo = '';
	    var listItemEnumerator = collListItem.getEnumerator();		
	
		var i = 0;
	    while (listItemEnumerator.moveNext()) {
	        var oListItem = listItemEnumerator.get_current();

			
	        htmlTbl += "<tr><td class='NRM_pj_align'>" + oListItem.get_item('ID') + "</td>" +
    		"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Title')) + "</td>"+
			"<td class='NRM_pj_align' data-order="+oListItem.get_item('Faciltity_x0020_Priority')+"> "+
            "<input type='text' size='1' disabled name = 'RowID-" + oListItem.get_item('ID') + "' id='faRowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('Faciltity_x0020_Priority') + " value=" + checkForNull(oListItem.get_item('Faciltity_x0020_Priority')) + ">"+
            "</td>" +
            
			"<td class='NRM_pj_align' data-order="+oListItem.get_item('VISN_x0020_Priority')+"> "+
            "<input type='text' size='2' disabled name = 'RowID-" + oListItem.get_item('ID') + "' id='viRowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('VISN_x0020_Priority') + " value=" + checkForNull(oListItem.get_item('VISN_x0020_Priority')) + ">"+
            "</td>" +
            
			"<td class='NRM_pj_align' data-order="+oListItem.get_item('National_x0020_Priority')+"> "+
            "<input type='text' size='2' disabled name = 'RowID-" + oListItem.get_item('ID') + "' id='naRowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('National_x0020_Priority') + " value=" + checkForNull(oListItem.get_item('National_x0020_Priority')) + ">"+
            "</td>" +
            				        
			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Project_x0020_Number')) + "</td>"+	        
			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('VISN')) + "</td>" +
			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Station_x0020_Name')) + "</td>" +

			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Total_x0020_Project_x0020_Cost')) + "</td>"+	        
			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Design_x0020_Obligation_x0020_Da')) + "</td></tr>";
			//"<td>" + oListItem.get_item('Construction_x0020_Obligation_ ') + "</td></tr>";

		}
		
			htmlTbl += "</tbody></table>";
		
			$("#divListItems").html(htmlTbl);
			

	 	/*Apply DataTable style */
     	var table = $('#prioritize').DataTable();     
	    // Event listener to the two range filtering inputs to redraw on input
	    $('#min, #max').keyup( function() {
	        table.draw();
	    } );  

	
	}
	
	$.fn.dataTable.ext.search.push(
	    function( settings, data, dataIndex ) {
	        var min = parseInt( $('#min').val(), 10 );
	        var max = parseInt( $('#max').val(), 10 );
	        var totProjCost = parseFloat( data[8] ) || 0; // use data for the age column
	 
	        if ( ( isNaN( min ) && isNaN( max ) ) ||
	             ( isNaN( min ) && totProjCost <= max ) ||
	             ( min <= totProjCost && isNaN( max ) ) ||
	             ( min <= totProjCost && totProjCost <= max ) )
	        {
	            return true;
	        }
	        return false;
	    }
	);
	
	
	function checkForNull(val) {
		if (val === null) {
			return " ";
		}else {return val;}
	}

Open in new window

0
Comment
Question by:Isaac
  • 4
  • 4
8 Comments
 
LVL 5

Author Comment

by:Isaac
Comment Utility
Here's my html file

<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">

<script src="/Webpart/JavaScript%20Library/js/jquery.js"></script>

<link rel="stylesheet" type="text/css" href="/Webpart/JavaScript%20Library/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="/Webpart/NRM/projectModule.css">
<script type="text/javascript" src="/Webpart/JavaScript%20Library/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/Webpart/JavaScript%20Library/js/jquery.SPServices-2014.02.js"></script>
<script type="text/javascript" src="/Webpart/NRM/priorityModule.js"></script>

<div id="divListItems"></div>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Place an alert to confirm :
alert( "found: " + $('[id^=faRowID]').length + " and its value is: " + $('[id^=faRowID]').attr('disabled') );
$('[id^=faRowID]').removeAttr('disabled');

Open in new window


if you get at least 1 please send me your creds.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
also try this :
jQuery('[id^=faRowID]').removeAttr('disabled');
instead :
$('[id^=faRowID]').removeAttr('disabled');
0
 
LVL 5

Author Comment

by:Isaac
Comment Utility
Neither one of those worked.  The alert never generated only when I went into developer mode (f12). See image below:

alert
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
so found 0, if no element found, how do you want to remove the disabled attribute ?
as you can see, the browser need a bit of time to draw/generate the table.
the bad way is to add a timer :
So you replace :
jQuery('[id^="faRowID"]').removeAttr('disabled');

Open in new window

by :
setTimeout(function() { 
   $('[id^="faRowID"]').removeAttr('disabled'); 
}, 5000); // wait 5 seconds before removing the disabled attribute

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
look like you find the good way and use SP.ClientContext.executeQueryAsync(succeededCallback, failedCallback) which throw once the table is completed and drawed, could you please close the question?
For any further reference : https://msdn.microsoft.com/en-us/library/office/dn168907.aspx
0
 
LVL 5

Author Comment

by:Isaac
Comment Utility
It's still not behaving the way I would hope.  When you go to the next page, the boxes become disabled.  I will close the ticket though and open up another question.  Thanks!
Adding the double quotes helped with a slight variation.

jQuery('input[id^="faRowID"]').removeAttr('disabled');
0
 
LVL 5

Author Closing Comment

by:Isaac
Comment Utility
jQuery('input[id^="faRowID"]').removeAttr('disabled');
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now