0

I am using CKeditor and this code is for a button. I store xml in Dom. I am trying to use the xml in the other function for a select box. The select box's code is similar with this button code. when I hit a list in the select box, i want to see the word between <chr> and </chr>. I do not want to put <chr> in a option.

type: 'button',
id: 'btnFind',
align: 'left',
style: 'width:100%',
label: 'add',
onClick: function () {
    var y = this.getDialog();
    var value1 = y.getValueOf('replace', 'chgWord');
    var value2 = y.getValueOf('replace', 'title');
    var xml = "<channel><title>" + value1 + "</title><chr>" + value2 + "</chr></channel>",
    xmlDoc = $.parseXML(xml),
    $xml = $(xmlDoc),
    $title = $xml.find("title");
    $("#cke_119_select").append("<option value='" + $title.text() + "'>" + $title.text() + "</option>");
    $title = $xml.find("chr");

this is a code for selecbox:

type: 'hbox',
widths: ['200px', '90px'],
children: [{
    type: 'select',
    label: '약어 LIST :',
    size: 5,
    style: 'width:100%',
    labelLayout: 'horizontal',
    id: 'list',
    items: [],
    onClick: function () {

        selValue = $("#cke_119_select option:selected").val();
        selText = $("#cke_119_select option:selected").text();
        selIndex = $("#cke_119_select option").index($("#cke_119_select option:selected"));
        selBox = $("#cke_119_select");
        selOption = selBox.attr("options");

        var y = this.getDialog();
        var value1 = y.setValueOf('replace', 'chgWord');
        value1.setValue(selValue);
Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
Albright
  • 109
  • 2
  • 12
  • 1
    what is your question? Please be specific since code alone makes little sense – charlietfl Nov 07 '12 at 02:36
  • I want to use "var" in the other function. Since the code for parseXML only work in the function for the button, I can not use "var xml" int the other function for select. – Albright Nov 07 '12 at 05:36

2 Answers2

1

The var is a JavaScript keyword and is independent from where it is being used and you can use it as you wish.

If you're in a function then "var" will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it). https://stackoverflow.com/a/1470494/694325

You already even have var in both of the onClick functions, so you indeed can and Do use it already.

var xml = "<channel><title>" + value1 + "</title><chr>" + value2 + "</chr></channel>",
    xmlDoc = $.parseXML(xml),
    $xml = $(xmlDoc),
    $title = $xml.find("title");

That for example creates four variables at the same time, like var a=1,b=2,c=3;

var y = this.getDialog();

That is exactly the same way of declaring variables. Your issue is not with var here.

It looks like you want to set the value/values for a richCombo. Maby you could try functions like var theValue = this.getValue(); and this.setValue("New Value"); inside the onClick. Also, onClick: function ( value ) { ... alert(value); ...} might be useful. If you want to change the items try accessing this._.items in the onClick. I can't help you more without knowing more on exactly what you are trying, sorry!

Community
  • 1
  • 1
Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • Thank yuo so much. It's so helpful for me. I used localStorage to do this. you answer gave me more information! – Albright Nov 08 '12 at 00:59
1

If you declare a var outside of the first function you can use the variable in the second

var xml;

function one(){
  xml= "<channel><title>" + value1 + "</title><chr>" + value2 + "</chr></channel>";

}

function two(){
  /* can access xml here that was set in function one*/
}
charlietfl
  • 170,828
  • 13
  • 121
  • 150