vamsi
vamsi

Reputation: 1586

Call a function on href

Below is my script

 $('<a class="page_link" href="javascript:'+getResults('http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page=' + i)+'" longdesc="0" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link'); 

Like above am trying to call a function on href attribute. But is is showing undefined on href.

What is the proper way of doing this?

Upvotes: 1

Views: 87

Answers (5)

Satpal
Satpal

Reputation: 133453

I would recommend to create HTML using jQuery and bind event using it. Refer to jQuery( html, attributes )

$('<a />', {
    'class' : 'page_link', 
    'longdesc' : 0,
    'text' : i  
}).css({
    'display' : 'inline-block'
}).on('click', function(){
    getResults('http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page=' + $(this).text());
})
.insertBefore('.next_link'); 

Upvotes: 1

Oleg Belousov
Oleg Belousov

Reputation: 10111

Instead of binding the event on every added element, you can create one event on the container, or the body:

In the ready function:

$('body').on('click', 'a.page_link', function (e) {
    e.preventDefault();
    getResults('http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page=' + $(this).text());
});

Whenever you want to insert a new element:

$('<a class="page_link" href="#" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link'); 

Upvotes: 1

Deepak Biswal
Deepak Biswal

Reputation: 4320

Here is a working demo.

HTML

<div id="hello"></div>

JS

$('<a/>', {
    href: '#',
    class: 'page_link',
    html: 0,
    click: function() { alert("Hi"); }
}).appendTo($('#hello'));

Hope this will help you.

NOTE: html is the text you want to show a hyperlink.

Upvotes: 0

vijayP
vijayP

Reputation: 11512

You can try below code:

 $('<a class="page_link" href="javascript:getResults(\'http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page='+  i+'\');" longdesc="0" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link');

Upvotes: 0

BenG
BenG

Reputation: 15164

your close, you just need to update your href to:-

$('<a class="page_link" href="javascript:getResults("http://192.168.1.122:10039/wps/mycontenthandler/vamshi/!ut/p/digest!SqaCnIAqyulFhaUStKGgJQ/searchfeed/search?query=*&scope=1440172377201&start=0&results=1000&pageSize=2&page=' + i + '") longdesc="0" style="display: inline-block;">' + i + '</a>').insertBefore('.next_link');

you are mixing the ' & ".

Upvotes: 0

Related Questions