How to change onclick attribute on html file?

Discussion in 'JavaScript' started by Megamon, Jun 20, 2014.

  1. #1
    Hey guys, i need your help.

    This is what i want to do , i got this progress bar and i would like when i click the (link) to change the attribute of number (80) to 100:

    data-pro-bar-percent="80" --> data-pro-bar-percent="100"

    This is the code:

    <a class="button" href="#">Click Link</a>
        <div class="pro-bar-container color-green-sea">
                        <div class="pro-bar bar-100 color-turquoise" data-pro-bar-percent="30" data-pro-bar-delay="4000">
                            <div class="pro-bar-candy candy-ltr"></div>
                        </div>
                    </div>
    
    Code (markup):
     
    Megamon, Jun 20, 2014 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    If you don't have any other pro-bar's then this should work:

    
    $('.pro-bar').css('width', '50%');
    
    Code (markup):
    If you do have others, then you might want to add an ID attribute to the div and access it as so, otherwise all the other pro-bar's will get the same width.
     
    ThePHPMaster, Jun 20, 2014 IP
  3. Megamon

    Megamon Member

    Messages:
    80
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    43
    Digital Goods:
    1
    #3

    Where do i post that what do i do? Yeah i don't have any other probar elements:)

    Would you please tell me ? where i post this code?

    Do i put them on script element inside in the head or above of the end of </body> tag?
    Do i create a new file and save it and redirect with script src?
     
    Megamon, Jun 22, 2014 IP
  4. rehan.khalid.9235

    rehan.khalid.9235 Active Member

    Messages:
    78
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    85
    #4
    here is simple solution using javascript. i have given 'id' to the target div.

    
    
    <a class="button" onclick="changeProgressBar()">Click Link</a>
        <div class="pro-bar-container color-green-sea">
                        <div id="pBar" class="pro-bar bar-100 color-turquoise" data-pro-bar-percent="30" data-pro-bar-delay="4000">
                            <div class="pro-bar-candy candy-ltr"></div>
                        </div>
                    </div>
    HTML:
    <script>
    function changeProgressBar() {
    var valueToSet=80;
        var pBar=document.getElementById('pBar');
        pBar.setAttribute('data-pro-bar-percent',valueToSet);
    }
    <script/>
    Code (JavaScript):
     
    rehan.khalid.9235, Jun 22, 2014 IP