Solved

JQUERY Add/remove class Issue - During Page Load

Posted on 2013-01-10
4
570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

726 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