0

I need to take value, when click on particular link which has id="username" but it does not work...

My attempt:

function rev() {
    var username = (this).getElementById("username").textContent;
    return alert (username);
}
while($trackResultRow = mysqli_fetch_assoc($trackResult)){?>

    <a onclick="rev()"><span class="glyphicon glyphicon-thumbs-down pull-right"></span></a>
    <span class="glyphicon glyphicon-thumbs-up pull-right"></span>
    <li>
        <a href="<?php echo $trackResultRow['track_path']?>"><span id="username"><?php echo $trackResultRow['username']?></span> - <span id="track"><?php echo $trackResultRow['track_name']?></span>

            <span class="glyphicon glyphicon-download pull-right"></span>
            <div class="pull-right">
                <span class="glyphicon glyphicon-pause pull-right" onclick="document.getElementById('aud').pause()"></span>
                <span class="glyphicon glyphicon-play pull-right" onclick="document.getElementById('aud').play()"></span>
            </div>
        </a>
    </li>
    <hr>
<?php
    }
?>

Also I tryed

function rev() {
   var username = document.getElementById(value).textContent;

   return alert (username);
}


 <button id="but" onclick="rev()">hey</button>
 <button id="but" onclick="rev()">hey233</button>

Any ideas?

4 Answers4

1

If you want to target an element on page, use document.getElementById

document.getElementById("username").textContent;
Elīna Legzdiņa
  • 307
  • 1
  • 3
  • 11
  • does not work it takes the value of first element only and it does not matter what element was clicked – Pavel Shilyaev Apr 19 '17 at 17:06
  • I tryed to apply it to more simple example ......But the result in both cases is HEY – Pavel Shilyaev Apr 19 '17 at 17:06
  • @PavelShilyaev You should only have one element with each ID. IDs are meant to be unique; If you need multiple elements with the same selector, use classes instead. With multiple IDs, only the first one will ever be targeted. – freginold Apr 19 '17 at 17:11
0

Use this for taking value from a html.

var value = document.getElementById('id').value;

If you want to get a specific value stored in a variable that is in your PHP code.

<input type = "text" id = "id" name = "var_PHP" value = "<?php echo $var_php;?>">
Jose Marques
  • 748
  • 1
  • 6
  • 22
0

There are two things you can do,

1) Assign different ID to the each element found by $trackResultRow['username'] by adding an incrementing variable like username_1, username_2 and then getElementById for each ID putting that in a for loop.

2) Assign a Class instead of ID and getElementsByClass and iterate through the result and get the value by using this how to get value by class name using javascript

getElementsById() will only give you first element.

Community
  • 1
  • 1
Neetigya
  • 121
  • 2
  • 10
0

id in attribute tag html must different. Try this

<!DOCTYPE html>
<html>
<body>


<button id="items" onclick="myFunction(this.id)">Try it</button>
<button id="item" onclick="myFunction(this.id)">Try it again</button>

<p id="demo"></p>
<script>
function myFunction(id) {
    var x = document.getElementById(id).textContent;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Gangga
  • 26
  • 1
  • 3