Solved

JQUERY Add/remove class Issue - During Page Load

Posted on 2013-01-10
4
566 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (http://www.cybersecurityventures.com/cybersecurity-market-report), worldwide spending on cybersecurity …
February 24, 2017 — On February 23, Travis Ormandy, a vulnerability researcher at Google, reported on Twitter (https://twitter.com/taviso/status/834900838837411840) that massive stores of data have been leaked by CloudFlare, a company that provide…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 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