13

Script

 <a href="#?cat=MT&typ=1" data-reveal-id="reveal_popup" onclick="pop();" data-closeonbackgroundclick="false" data-dismissmodalclass="close-reveal-modal">due today</a>
 <a href="#?cat=MT&typ=2" data-reveal-id="reveal_popup" onclick="pop();" data-closeonbackgroundclick="false" data-dismissmodalclass="close-reveal-modal">due in 2 days</a>

 <div id="reveal_popup" class="reveal-modal">
 <div id="popup"></div>
 <a class="close-reveal-modal">&#215;</a>
 </div>

function pop() {

 var cat=**value of cat parameter in href**
 var type=**value of typ parameter in href**

 $.ajax({
       type:'post',
       url:site_url()+'/event/deleteEventSingle',
       data:{'cat':cat,'type':typ},
        async:false,
         success:function(result){}
        });
}

In this when the user clicks a href same popup appears with different data. there are more than 10 hrefs actually and i am trying to show a calender with user inputted data in the popup. This depends on two parameters cat and typ as shown in href.

Requirement

Every href has its own cat and typ values. When a href is clicked I want the cat and typ values of the clicked href to be get using jquery so that i can pass these variables in ajax.

var cat=**value of cat parameter in href**
var type=**value of typ parameter in href**

Tried

How to get the parameters of an href attribute of a link from the click event object

3
  • 1
    i suggest you use data-* attributes. like this ` <a href="#" data-reveal-id="reveal_popup" onclick="pop();" data-closeonbackgroundclick="false" data-dismissmodalclass="close-reveal-modal" data-category="MT" data-cat-type="1">due today</a> `. Commented Apr 3, 2013 at 6:58
  • @Ravi: how can I get the values using this Commented Apr 3, 2013 at 7:10
  • you can use jquery data() method. Commented Apr 3, 2013 at 7:35

2 Answers 2

17

You can do as soeme thing as below

$('a').bind('click',function(){
    var url = ($(this).attr('href'));
    var cat = getURLParameter(url, 'cat');
    var typ = getURLParameter(url, 'typ');
    //calling the ajax function
    pop(cat, typ)
});


function getURLParameter(url, name) {
    return (RegExp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1];
}

function pop(cat, typ) {

    $.ajax({
       type:'post',
       url:site_url()+'/event/deleteEventSingle',
       data:{'cat':cat,'type':typ},
        async:false,
         success:function(result){}
    });
}

Check out the the example at Live fiddle http://jsfiddle.net/mayooresan/aY9vy/

Sign up to request clarification or add additional context in comments.

2 Comments

did you check the fiddle when you click on the link it returns the correct value..
Can you Please have the same without ajax I asked the same in Below Link Please help me out stackoverflow.com/questions/45663358/…
7

Try this :

function getUrlParams(url) {
    var params = {};
    url.substring(1).replace(/[?&]+([^=&]+)=([^&]*)/gi,
            function (str, key, value) {
                 params[key] = value;
            });
    return params;
}

Usage:

function pop(e) {
     var url = $(e.target).attr("href");
     var params = getUrlParams(url); 

     var cat= params["cat"];
     var type= params["typ"];

     alert(cat);
     alert(type);    
}

Working Fiddle

2 Comments

i am getting value as undefined when i alert cat
Is the substring call needed? Seems to me its just bypassing the # portion of the URL in your demo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.