0

I have a working jquery function which I can call like this through php

if (!order) 

        {   echo '<script>
$(function() {
$this = $("#ui-accordion-accordion-panel-0");
//  parent tab
var parent = $("#accordion").parent();
//parent.adderror();

// the content
$this.adderror();

// the header
var header = $this.attr("aria-labelledby");
$("#"+header).adderror();


// the tab
var tab = parent.attr("aria-labelledby");
$("#"+tab).parent().adderror();
});

    </script>';}

I want to make this a global function (even better if defined externally in a .js file) that I can call from wherever I want in my php, which takes the panel as a parameter.

So I would basically call - show_error("#ui-accordion-accordion-panel-0"). How could I do this? When I tried defining function, and then calling it - there was no change whatsoever. Was using through techinques defined here to no avail (var functionName = function() {} vs function functionName() {})

Thanks in advance

Ok, I have now an error.js file, which I have linked appropriately ( )

function show_error(selector) {
var $this = $(selector);

//  parent tab
var parent = $("#accordion").parent();

//parent.adderror();

// the content
$this.adderror();

// the header
var header = $this.attr("aria-labelledby");
$("#"+header).adderror();


// the tab
var tab = parent.attr("aria-labelledby");
$("#"+tab).parent().adderror();
}
3
  • FWIW, your function falls prey to The Horror of Implicit Globals because you don't declare your $this variable. Commented Feb 27, 2013 at 11:37
  • What's parent in the line var tab = parent.attr("aria-labelledby");? You haven't shown anything showing where you're getting that. Commented Feb 27, 2013 at 11:38
  • My bad, thought I would just post the snippet. Ended up confusing you all, I'm sorry. Just made the relevant edit now. Commented Feb 27, 2013 at 11:43

1 Answer 1

1

The function in your external .js file would look like this:

function show_error(selector) {
    var $this = $(selector);
    //  parent tab
    var parent = $("#accordion").parent();
    //parent.adderror();

    // the content
    $this.adderror();

    // the header
    var header = $this.attr("aria-labelledby");
    $("#"+header).adderror();

    // the tab
    var tab = parent.attr("aria-labelledby");
    $("#"+tab).parent().adderror();
}
Sign up to request clarification or add additional context in comments.

10 Comments

I tried something very similar, didn't work. Will try again now.
@PratikBothra: Well, again, there's the question of where you're getting parent. But fundamentally, that does work, barring other issues on your page not shown in your question.
To help debug the code, I added an alert($this) in the function, and this is what I get - [object Object] as the alert. So the function is being called, but no change is happening at the moment. Pretty sure the alert message, shouldn't show that, or should it?
@PratikBothra: It should, $this is a jQuery instance (object), since it's the return value of jQuery's $ function. That's why you can call methods on it.
alert(selector) is giving me #ui-accordion-accordion-panel-0, if that helps
|

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.