Unable to trigger click event on link

My problem is that I got two aspx controls created as follows:

<a id="sortByDate" href="javascript:__doPostBack('sortByDate','')">Date</a> <a id="sortByLastName" href="javascript:__doPostBack('sortByLastName','')">Last name</a> 

so that these links allow you to sort the results. I am trying to put this in a combobox instead of using links.

So I made it like this

 <select id="sortBySelect" onchange="javascript:sortBy(this);"> <option value="sortByLastName">Last name</option> <option value="sortByDate">Date</option> </select> 

using this javascript function

 function sortBy(sel) { var id = sel.value; $("#" + id).trigger("click"); } 

Therefore, when you change the selected item in combobox, I want to trigger the click event on the link to trigger the dopostback sort.

He’s not doing anything yet. I tried "click", "onclick", "onClick" and nothing works. Unfortunately, this is IE quirks mode.

I know this is really not elegant, but I have a very short time and I need something quick and dirty. In the end, I will be able to control aspx to handle this.

Any ideas how I could make this work in quirks mode?

thanks

+4
source share
4 answers

Try changing the page layout:

 document.location = $("#" + id).attr('href'); 
+4
source

why don't you just start __doPostBack directly:

 function sortBy(sel) { var id = sel.value; __doPostBack(id,''); } 
+2
source

Using:

function sortBy (sel) { var id = sel.value; alert ($ ("#" + id) .length); // only to match this element. $ ("#" + id) .click (); }

In fact, jQuery only raises any event associated with an element. You are trying to invoke the href action.

If you do not want to change the markup, you can try this solution .

Otherwise, you will have to change the html markup to bind javascript events, and then try to run it.

Good luck

+1
source

Everything works as intended!

JSFiddle proof.

Make sure you have an element with the identifier sortById and sortByLastName !


Javascript / jquery

 $(document).ready(function(){ $('#sortByDate').click(function(){ alert('Sort By Date Click Event fired!'); }) }); function sortBy(sel) { var id = sel.value; $("#" + id).trigger("click"); } 

Alternative

Change window.location change

Javascript / jquery

 function sortBy(sel) { var id = sel.value; window.location = $("#" + id).attr('href'); } 

Note. You will not see any changes in JSFiddle:

 Refused to display 'https://www.google.ca/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. 
+1
source

Source: https://habr.com/ru/post/1488489/


All Articles