NewbieCoder
NewbieCoder

Reputation: 7

href link not working while using jquery trigger

i have a javascript like this one

var funct = function(){
return {
    init : function(data) {                 
        this.sendRequest({
            action : 'login'
        });
    },      
    login : function()
    {           
        var parentDIV = $("#dnn_ctr450_HtmlModule_lblContent");
        parentDIV.attr('href', 'https://webcenter.elendersolutions.com/webcenter/').trigger('click');               
    }
   }
}

HTML CODE of this one.

<div id="dnn_ctr450_ModuleContent" class="DNN_HTMLContent">

<div id="dnn_ctr450_HtmlModule_lblContent" class="Normal">
    <p></p>
    <h2></h2>
    <h2>
        <a href="https://webcenter.elendersolutions.com/webcenter/" target="_self"> <-- how can i trigger this one?
            <img width="188" height="40" border="0" src="/Portals/0/WebcenterSignin.gif" alt="Webcenter Login"></img>
        </a>
    </h2>
    <h2>
        <a href="https://rates.lsi-lps.com/">
            <img width="188" height="40" border="0" src="/Portals/0/RateCalculatorSignin.gif" alt=""></img>
        </a>
    </h2>
    <h2></h2>
    <p></p>
</div>

what i'm trying to do is to trigger the link so it would function like i just click it manually.

Upvotes: 0

Views: 190

Answers (2)

nbrooks
nbrooks

Reputation: 18233

For one thing, you aren't actually selecting the link correctly. Rather, you are selecting its parent div, and setting an href on the div. The target element of your function is what is in your jQuery object, in this case the div: $("#dnn_ctr450_HtmlModule_lblContent").

Instead, select the anchor link directly (it may be worth reviewing the jQuery docs for the attribute-equals selector and find:

var parentDIV = $("#dnn_ctr450_HtmlModule_lblContent");
var aLink = parentDIV.find("a[href='https://webcenter.elendersolutions.com/webcenter/']");

The second problem is that jQuery's click functionality won't allow you to follow links; you'll have to take advantage of the native html element's click method, as detailed in this SO post.

aLink[0].click();

Note that by doing [0] we are accessing the underlying DOM element, and so the method we're calling is not a jQuery method.

Upvotes: 1

Netorica
Netorica

Reputation: 19327

we can't do anything with it with JQuery. the only we can do is to progmatically do the redirection.

var href = parentDIV.attr('href');
window.location.href = href;

but with native javascript yes we can.

parentDIV.[0].click(); //where the index 0 returns the native DOM object.

but please be advised that for some reasons browsers doesn't want to trigger redirection from href link when triggered progmatically by javascript.

Upvotes: 0

Related Questions