javascript - Change part of link with jquery -
i using ransack sorting. couldn't find way change sorting links when search , sorting uses ajax. developing own solution change link , other stuff.
so far have this
ruby
<%= search_form_for @search, :class=>"search",:id=>"search-menio",:remote=>"true", url: advertisements_path, :method => :get |f| %> <div class="sort-link-css"> <%= sort_link(@search, :price,"cena", {},{ :remote => true, :method => :get }) %> </div> <%end%>
that generates :
<div class="sort-link-css"> <a class="sort_link asc" data-method="get" data-remote="true" href="/lv/advertisements?q%5bs%5d=price+desc">cena ▲</a> </div>
my goal:
when user clicks on link change link class represents sorting order , end of link represents sorting order.
asc -> desc desc -> asc
and submits search form.
problem: first click see blinking , changes, when try second click nothing happens.
my script:
$('.sort-link-css > a').click(function() { var classname = $(this).attr('class'); if (classname = "sort link asc") { $(this).removeclass('sort link asc'); this.href = this.href.replace('desc', 'asc'); $(this).attr('class', 'sort link desc'); } else if (classname = "sort link desc") { $(this).removeclass('sort link desc'); this.href = this.href.replace('asc', 'desc'); $(this).attr('class', 'sort link asc'); } $('.search').submit(); })
what doing wrong ?
ideally use jquery.hasclass
or jquery.is
determine if element has specified class or classes.
you need update display before navigation or form submit. can this:
$('.sort-link-css > a').click(function(e) { // cancel click event on link e.preventdefault(); var $this = $(this); if ($this.is('.asc')) { this.href = this.href.replace('desc', 'asc'); } else if ($this.is('.desc')) { this.href = this.href.replace('asc', 'desc'); } $this.toggleclass('asc desc'); settimeout(function() { // not sure if want load link href // or submit form; use 1 of following window.location = $this.get(0).href; $('.search').submit(); }, 100); });
Comments
Post a Comment