Solved

JQUERY Add/remove class Issue - During Page Load

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PayPal Discount Code 8 198
Jquery Ajax error Origin is not allowed by Access-Control-Allow-Origin. 6 1,326
Checkbox 4 369
CRM 2011 Opportunity Bug 18 147
Arrow Electronics was searching for a KVM  (Keyboard/Video/Mouse) switch that could display on one single monitor the current status of all units being tested on the rack.
In order to fulfill our mission of inspiring learning in the technology community, Experts Exchange is launching a Course of the Month program. Premium and Team Account members will have access to one course per month as a part of their membership, …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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