Buying js problem

Discussion in 'Programming' started by secretdesign, Apr 4, 2013.

  1. #1
    Hi,

    here is what i have.
    
     
    <script>
    function myFunction()
    {
    document.getElementById("id").innerHTML="<?php echo $id ?>";
    }
    </script>
    <a class="play" onclick="myFunction()"></a>
    <span id="id"></span>
     
    
    Code (markup):
    I need to get id, like 678 and not <span id="id">678</div>

    THX
     
    secretdesign, Apr 4, 2013 IP
  2. eli03

    eli03 Well-Known Member

    Messages:
    2,887
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    175
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    better change the id to class or change the value id.
     
    eli03, Apr 4, 2013 IP
  3. secretdesign

    secretdesign Well-Known Member

    Messages:
    797
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    3
    As Seller:
    100% - 4
    As Buyer:
    100% - 0
    #3
    Thx for tip but this isn't problem.
     
    secretdesign, Apr 4, 2013 IP
  4. eli03

    eli03 Well-Known Member

    Messages:
    2,887
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    175
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    this will replace the value id with variable id 678, use attr
    var id= 678;
    $('span').attr('id', id);
    Code (markup):
     
    eli03, Apr 4, 2013 IP
  5. secretdesign

    secretdesign Well-Known Member

    Messages:
    797
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    3
    As Seller:
    100% - 4
    As Buyer:
    100% - 0
    #5
    Thx but this didn't work.

    I have change code to work in wp.

    
     
    <script type="text/javascript">
    $(document).ready(function() {
        $('.play-<?php echo $id ?>').click( function() {
            $('#id').html("<?php echo $id ?>");
        });
    });
    </script>
    <a class="play play-<?php echo $id ?>"></a>
    <span id='id'></span>
    
    Code (markup):
     
    secretdesign, Apr 4, 2013 IP
  6. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #6
    jQuery's documentation for selectors with multiple classes says something like this: $('.class1.class2') selects and element that has class="class1 class2". Also, jQuery expects .class selector to return an array of objects. So you can easily set that play-678 as an id="678" or if you can't change the id just use the .data('play') function corelating with data-play="678" (only if you already have an unique id for that element).

    I'm not sure what are you trying to accomplish, but hope it's the solution for you.
     
    edduvs, Apr 6, 2013 IP