Jack Billy
Jack Billy

Reputation: 7211

Why my Jquery function not working?

I have this jquery function ---

function domail_slide_settings(obj)
{
  $("div#" + obj).toggle();
}

and when I execute it like this ---

<a href="#" onclick="domail_slide_settings('toggle')" id="toggle" >Toggle</a>

It doesn't toggle! Does anyone have any ideas about this. Please let me know, it would be a great help.

Thanks in advance!

Upvotes: 0

Views: 118

Answers (5)

hunter
hunter

Reputation: 63522

Try this instead:

<a href="#" id="toggle">Toggle</a>
<div id="div_toggle"></div>

and this:

$(function() {
    $("#toggle").click(function(e){
        e.preventDefault();
        $("#div_" + this.id).toggle();
        // or $(this).next().toggle(); depending on your markup
    });
});

I bet you're using the same id for your <a> tag and your <div> tag which should not be done.

Upvotes: 1

Yoni
Yoni

Reputation: 10321

the result of your string concatenation - "div#toggle" - is selecting a div with id toggle, instead of your anchor.

I think you meant to select it as:

$("#toggle").parent()

or, if you want really what the anchor itself, use this:

$("#toggle")

In any case, since your function is an onclick handler (directly through the browser though, not using jQuery event api), you might as well do this:

function domail_slide_settings() {
    $(this).parent().toggle()
    // or this.toggle();
}

Upvotes: 0

Kent
Kent

Reputation: 2960

or you can try

$('#' + obj).parent().toggle();

^^

Upvotes: 0

Andy E
Andy E

Reputation: 344575

ID attributes must be unique in a HTML document, otherwise you will run into unwanted side-effects. From the looks of it, your <a> element with the click handler has the same id attribute as the div#toggle element you're toggling. This would almost certainly be a problem in some browsers.

Try changing the ID of your <a> element to be unique:

<a href="#" onclick="domail_slide_settings('toggle')" id="toggle_div" >Toggle</a>

Upvotes: 4

Marco Mariani
Marco Mariani

Reputation: 13766

try with

  $("#" + obj).toggle();

and point it to a < div > element, instead of an anchor

Upvotes: 4

Related Questions