Solved

JQUERY Add/remove class Issue - During Page Load

Posted on 2013-01-10
4
565 Views
Last Modified: 2013-01-15
Hi experts,
I am facing an interesting some scripting issue during page load event.
am calling the below JS function when i want to sort a table either in ASC or DSC.
I am achieving this functionality by submitting the form. i tried to to sort the table based on the table sorter in JQuery. it worked very well. however there was some alignment issue that made me to use the form submission method where i will sort everything behind the browser.

Here is the issue:

HTML form element TH and its attributes:
<th  id="participant_id" class="headerSortUp addLink" 
  style="width: 20%;height: 5px; word-wrap: break-word" 
  onClick="javascript:sort ();" > Name
</th>

Open in new window

When i call the below function
function sort (){ 
  var queryString='';
  if($("#participant_id").hasClass('minusLink')){
	alert("am here in edit list desc"); 
	$("#participant_id").removeClass( 'headerSortDown' ).addClass( 'headerSortUp' );
	$('#participant_id').removeClass( 'minusLink' ).addClass( 'addLink' );	
    document.Form.editListSortOrder.val='desc';
    queryString += "&editListSortOrder=" + document.Form.editListSortOrder.val; 
	document.Form.action = "editcontributions?"+queryString;
	document.Form.submit();
  }
  else if($("#participant_id").hasClass('addLink')){
	alert("am here in ascc");
	$("#participant_id").removeClass( 'headerSortUp' ).addClass( 'headerSortDown' );
	$("#participant_id").removeClass( 'addLink' ).addClass( 'minusLink' );
	document.Form.editListSortOrder.val='asc';
	queryString += "&editListSortOrder=" + document.Form.editListSortOrder.val; 
	document.Form.action = "editcontributions?"+queryString;
	document.Form.submit();
  }  
}

Open in new window

Whenever i submit the form through conditional check, the page renders with the sorted result and the TH class attribute loads with "headerSortUp" . i.e default class Attribute all the time. so i am unable to perform the other form sorting. i can only perform ASC or DSC always. i need to perform this by toggleing header up and down .
Because of the page load with the same class attribute , am stuck and clue less. my window.load event does not have anything. even i tried adding condtional check over there. However the default class attributes comes into picture all the time. please suggest.

Thanks
0
Comment
Question by:Easwaran Paramasivam
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 38767064
Try this. I assume that all fields have id="same as name"

Also please note

1) no need for javascript: pseudo label and for sure not when you have jQuery
2) document.form.formfield.val is wrong, it is .value on DOM/Form access and .val() in jQuery

function setSort(asc) { 
  var queryString="";
  var sortOrder = asc?"desc":"asc";
  $("#participant_id")
         .toggleClass('headerSortDown',asc)
         .toggleClass('headerSortUp',!asc )
         .toggleClass('addLink',asc)
         .toggleClass('minusLink',!asc);
  $("#editListSortOrder").val(sortOrder);
  queryString += "&editListSortOrder=" + sortOrder;
  $("#form1").attr("action","editcontributions?"+queryString);
}
$(function() {
  var sortOrder = (location.search.indexOf("asc")!=-1)?"desc":"asc";
  setSort(sortOrder);
  $("#participant_id").on("click",function() {
    setSort(sortOrder);
    $("#form1").submit();
  });
  
});

Open in new window

and
<th id="participant_id" style="width: 20%;height: 5px; word-wrap: break-word">Name</th>

Open in new window

0
 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 38768473
Hello,
Thank you very much for your prompt response. This is really good. However i was able to fix the issue by passing a hidden variable and check during the page load event.
Based on the value , i was able to toggle the classes. Unfortunately there was a typo also in the id  from my end.

Thanks a lot for this nice post.

Regards
Easwar
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 500 total points
ID: 38768895
That is what this one does

$(function() {
  var sortOrder = (location.search.indexOf("asc")!=-1)?"desc":"asc";
  setSort(sortOrder);

it reads the url but can also read a field
0
 
LVL 16

Author Closing Comment

by:Easwaran Paramasivam
ID: 38777433
Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change car movement from jumping to swapping 8 93
Proper Posting 1 128
nvd3.js license 3 816
Retrieving  tree structure  data from database using java script framework 8 70
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
An analysis of the phishing scam that has been affecting Google users, along with steps to take for protection, as well as what to do if you receive one of the emails.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

864 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

23 Experts available now in Live!

Get 1:1 Help Now