Solved

JQUERY Add/remove class Issue - During Page Load

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

10 Experts available now in Live!

Get 1:1 Help Now