views:

64

answers:

4

Hello, I have some javascript code and a button which already has an event/action assigned to it, and I want to add a second event to the button. Currently the relevant bit of code looks like this:

   events: {onclick: "showTab('"+n+"')"}, 

how do I amend this code so that it also increases the 'wdith' property of a div with class='panel', by a fixed amount e.g. 50px?

Thanks

EDIT:

I understand. Problem is I don't know how to change id, only classname. The div is constructed in javascript using buildDomModel as follws:

  this.buildDomModel(this.elements["page_panels"],
        { tag: "div", className: "tab_panel",
          id: "tab_panel",
          childs: [...]}
 )

But the id doesn't get changed to 'tab_panel'. HOWEVER, classname does get changed to 'tab_panel' which is why i tried to use getelementbyclass. So the div has no id and I don't know how to create one using the buildDomModel but I can give unique class. I have put the code in original post too.

A: 
Sarfraz
Thanks but when I try the first one I get the error $ not defined. THe button's primary function still works though, but width remains unaltered.
David Willis
The example sAc wrote assumes you are using JQuery. Change the $('div.panel').width('50px');"-part to what ever javscript function you need to perform and it should execute.
Falle1234
I have put the showtab function into the original post above. When I paste $('div.panel').width('50px'); into it i get error. Please advise me.
David Willis
@David Willis: See my updated answer please.
Sarfraz
sAc I followed the instructions by creating a unique id for the panel but when I upload to server and then click button I get error "document.getElementsById is not a function"
David Willis
its not document.getElementsById it is document.getElementById..
KoolKabin
@David Willis: Ok my bad i made a typo, please check now, i have fixed that in my answer.
Sarfraz
Not working sadly. I put<SCRIPT LANGUAGE="JavaScript" TYPE="TEXT/JAVASCRIPT">function setWidth(){ var div = document.getElementByClass('tab_panel'); div.setAttribute('width', '50px');}</SCRIPT>near the top before header and gave the div a unique class 'tab_panel'. I also put the setWidth() function in the events of the button as instructed. Could it not be working because the width attribute of the div with class 'tab_panel' is specified in the CCS file and not in the index.php? maybe the width attribute in the CCS file is overiding the setWidth function perhaps.. I dont know
David Willis
Instead of `document.getElementByClass('tab_panel');`, use `document.getElementById('tab_panel');` And give your div id like this: `<div id="tab_panel">`
Sarfraz
I understand. Problem is I don't know how to change id, only classname. The div is constructed in javascript using buildDomModel as follws: this.buildDomModel(this.elements["page_panels"], { tag: "div", className: "tab_panel", id: "tab_panel", childs: [...]})But the id doesn't get changed to 'tab_panel'. HOWEVER, classname does get changed to 'tab_panel' which is why i tried to use getelementbyclass. So the div has no id and I don't know how to create one using the buildDomModel but I can give unique class. I have put the code in original post too.
David Willis
@David Willis: see my update 2 please.
Sarfraz
Some success but still not quite working. I implement your update 2 and when I click the button it changes the div to this: <div class="tab_panel" width="50px">But the CCS file still has the width:690px; so the width isn't increased by 50px, it remains at 690px.
David Willis
What i want is, each time I click the button, the DIV increases by 50px. Please help me
David Willis
You did not specify the width should be increased but i have modified my answer in update 2 with that.
Sarfraz
Thanks. It says error is_ie not defined. Also, I don't know if I explained it well enough, but what your code does is set the width to 50px in the html but because the CCS has the width of the div set to 690px it stays at 690px. So the html tag is <div class="tab_panel" width="50px"> but it remains width 690px. So I want it to be 690+50px in length after once click. Then 690+50+50px after two clicks and so on. Thanks so much for your expert guidance, I am learning a lot and I think it's almost there.
David Willis
@David Willis: I think you should use the jquery solution shown at the end. The problem is that your function is generating same class name and id for the div. Try the jquery alternative. Make sure to include the jquery library in your page.
Sarfraz
@David Willis: I have modified the jquery code.
Sarfraz
Hello I tried the jquery you offered and put the jquery library in the html but it does not work. Looking at the html source I can clicking the button changes the div from <div class="tab_panel"> to <div class="tab_panel" style="">. Any ideas?
David Willis
@David Willis: See that i had modified my code after first edit, make sure that you are using the code that i have their for jquery now.
Sarfraz
Yes I tried the code again to make sure, but it's the same.
David Willis
Try replacing that line with this then `jQuery(this).css({width: (jQuery(this).width() + 50) + 'px'})`
Sarfraz
perfect that does what i need. thank you. one thing I have noticed though is say I click the button twice and add 100px to the div, when i refresh the browser page the div goes back to it's original length. Is there a way to keep it at the length adjusted by the button click? Thanks again for your help
David Willis
@David Willis: Nope that's not possible because width is added dynamically. If you could add it from CSS then it will persist but i don't think that is what you are looking for. So this is very basic when you add something dynamically to a page, it won't be there on page refresh.
Sarfraz
I have another button, and when I click on it, it adds a div and the div stays when I refresh the page. Maybe it stores in cookies. I know it is possible but probably not easy.
David Willis
Yes cookies can be an alternative
Sarfraz
Thanks I accept your answer. Good man.
David Willis
@David Willis: Thanks i thought you did not know how to do that :) That also makes your accept rate better and this way more and more people will respond to your questions :)
Sarfraz
A: 

instead of the function you can use this line of JQuery code:

 $('div.tab_panel').attr('style',"width:100px");

any way if you dont want it to be JQuery then this function should work :

function setWidth() {
        var divs = document.getElementsByTagName('div');

        for (var i = 0; i < divs.length; i++) {
            // check if this div has panel class 
            if (divs[i].getAttribute('class') === 'tab_panel') {
                // set the style now 
                divs[i].setAttribute('style', 'width:100px');
            }
        }
    } 

putting width attribute to div wouldnt work, you need to set its style,

so i expect any of the code i provided to work.

Stacker
Does that increase the width of the div by 100px or just SET the width to 100px? Because what I want is for the div to increase by e.g. 100px, everytime the button is clicked.
David Willis
A: 

oh no the one i sent early just set width to a new value this one should fix your problem :

 var x = $('div.tab_panel')[0].style.width.replace("px", "");
        x = x + 50;
        $('div.tab_panel').attr('style',  "width:" + x + "px");

ok here is a non JQuery Version :

        function setWidth() {
        var divs = document.getElementsByTagName('div');
        for (var i = 0; i < divs.length; i++) {
            if (divs[i].getAttribute('class') === 'tab_panel') {
                var x = divs[i].style.width.replace("px", "");
                x = x + 50;
                divs[i].setAttribute('style', 'width:' + x + 'px');
            }
        }
Stacker
Hi thanks for the effort but it doesn't quite work (I tried both). When I click the button once it sets the width of the div to 50px. If I then click it a second time it sets the width of the div to 5050px (so i think that is literally '50' written twice rather than 50+50). The div starts off in the style sheet being specified as 690px so after one click I want it 740px, after two clicks, 790px. I think you are close.
David Willis
so to make it clear clicking the button 3 times gives 505050px, 4 times 50505050px and so on, which isn't what I desire
David Willis
A: 

ok here it is :

JQuery:

        function Button1_onclick() {
        var x = $('div.tab_panel')[0].clientWidth;
        x = x + 50;
        $('div.tab_panel').attr('style',  "width:" + x + "px");
    }

non JQuert:

function setWidth() {
        var divs = document.getElementsByTagName('div');
        for (var i = 0; i < divs.length; i++) {
            if (divs[i].getAttribute('class') === 'tab_panel') {
                var x = divs[i].offsetWidth;
                x = x + 50;
                divs[i].setAttribute('style', 'width:' + x + 'px');
            }
        }
    } 

this should work

Stacker
Hi I tried this, it does work but doe some reason adds 58px rather than 50px. The other guy who has been helping me with this question below solved it simultaneously but managed to do it so it adds exactly 50px.When the browser is refreshed the div goes back to the original size though, if you know a way to keep it, please educate me. Thanks for your help I learn a lot.
David Willis