SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use script dynamically

    Hi!
    Look.
    I have such sequence
    A few divs with different names which are generated automatically

    <div id="<?php echo $oneid;>">

    then I have inside each this div input fields and
    button with next attributes

    <a id="buttonid" class="button"><span><?php echo $button; ?></span></a>
    </div>

    And below I have javascript which is applying only to one certain div class with name active so I have to make div with any id work when button was pressed.

    <script type="text/javascript"><!--
    $('#buttonid').bind('click', function() {
    $.ajax({
    url: 'index.php?route=path/to/update',
    type: 'post',
    data: $('.active input[type=\'text\'], .active input[type=\'hidden\'], .active input[type=\'radio\']:checked, .active input[type=\'checkbox\']:checked, .active select, .active textarea') ............

    This is the question how to make work this script after user press any $button?
    When on page is only one div it works fine.
    Next way doesnt work
    <a onclick="document.getElementById('<?php echo $oneid;?>').setAttribute('class', 'active');" id="buttonid" class="button"><span><?php echo $button; ?></span></a>

    Any ideas?

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Your javascript (which is JQuery, by the way) applies to an ID of "buttonid".
    A page can only have one instance of a CSS ID (an ID must be unique).
    That answers your question.

    If you want to apply this to all buttons, I suggest you define them with the same unique CSS class and modify your JQuery to 'bind' to that class. In that case, avoid applying a CSS ID to the button(s) in your PHP output because you will have the same ID duplicated on the page.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow Parkin its a point. I passed by it without notice. I dont know how I will reorganize my code but after it I promise to write here.

  4. #4
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you help?
    This doesnt work
    Code:
    <a onclick="document.getElementById('<?php echo $oneid;?>').setAttribute('class', 'active'); this.setAttribute('id', 'buttonid');"  class="button"><span><?php echo $button; ?></span></a>

  5. #5
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Abandon the idea of using an ID altogether. And modify your original code like this:
    Code:
    $('.buttonstyle').bind('click', function() {
    ...
    This assumes you have a CSS class "buttonstyle" defined {you can name it as you please}
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  6. #6
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works fine as you've advised. Respect to you Parkin.
    I plan to learn javascript thoroughtly.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •