Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get title of onclick button inside javascript function once it is clicked?

I got few onclick buttons inside li . I wonder how I can get the title of each button inside JavaScript function once it is clicked ?I want to set the title of button equal to a variable.Thanks in advance.

<javascript>
    function MyFunction(MyFile,MyImage){

    //here i want to get the title of onclick button which is Mango and set it equal to variable.

    }
    </javascript>

    <li><a id="123456" onclick="setCurrentID('123456');javascript:MyFunction('type=fruit&day=friday','http://somesite.com/Image1.png');">Mango</a><img src="http://www.somesite.com/123456.png"></li>
    <li><a id="123457" onclick="setCurrentID('123457');javascript:MyFunction('type=fruit&day=friday','http://somesite.com/Image2.png');">Apple</a><img src="http://www.somesite.com/123457.png"></li>
like image 389
user1788736 Avatar asked Dec 02 '25 09:12

user1788736


1 Answers

You send the id with the onclick function and then you reference the label via:

document.getElementById("myBtn").text;

So basically:

<a id="1" onClick="reply_click(this.id)">B1</a><br />
<a id="2" onClick="reply_click(this.id)">B2</a><br />
<a id="3" onClick="reply_click(this.id)">B3</a>

<script type="text/javascript">
var newText = 'Thank You';
function reply_click(clicked_id)
{
    alert(document.getElementById(clicked_id).text);
    document.getElementById(clicked_id).text = newText;
}
</script>

Edit: Apologies, didn't see you were using anchors instead of buttons as your question mentions, my fault. I also added how to change the text.

like image 176
Munsterlander Avatar answered Dec 03 '25 23:12

Munsterlander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!