Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclick event pass <li> id or value

I want to pass <li> id or value in onclick event. here is my exiting code.

<li onclick="getPaging(this.value)" id="1" value="1">1</li>
<li onclick="getPaging(this.value)" id="2" value="2">2</li>

here is the javascript code

function getPaging(str)
{
$("#loading-content").load("dataSearch.php?"+str, hideLoader);
}
like image 295
Sasindu H Avatar asked Jun 08 '11 09:06

Sasindu H


People also ask

How do you pass a value on onClick React?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...

How do you pass dynamic ID on onClick function React?

How do you pass dynamic ID on onClick function in react JS? You can do this as follows : class Test extends React. Component { constructor(props){ super(props); this. state = { items: [ {item: "item", id: 1}, {item1: "item1", id: 2} ] } } handleClick(id, e){ alert(id); } render(){ return( <ul id="todo"> {this.

What is the correct way to pass the parameter ID to handleClick?

items. map((item,i) => <li className='list-group-item' key={i} data-id={item.id}>{item.name} <button onClick={this. handleClick.

Can you use onClick in tags?

It supports all HTML elements except <html>, <head>, <title>, <style>, <script>, <base>, <iframe>, <bdo>, <br>, <meta>, and <param>. It means we cannot apply the onclick event on the given tags. In HTML, we can use the onclick attribute and assign a JavaScript function to it.


3 Answers

Try like this...

<script> function getPaging(str) {   $("#loading-content").load("dataSearch.php?"+str, hideLoader); } </script>  <li onclick="getPaging(this.id)" id="1">1</li> <li onclick="getPaging(this.id)" id="2">2</li> 

or unobtrusively

$(function() {   $("li").on("click",function() {     showLoader();     $("#loading-content").load("dataSearch.php?"+this.id, hideLoader);   }); }); 

using just

<li id="1">1</li> <li id="2">2</li> 
like image 156
Anish Avatar answered Oct 06 '22 00:10

Anish


<li>s don't have a value - only form inputs do. In fact, you're not supposed to even include the value attribute in the HTML for <li>s.

You can rely on .innerHTML instead:

getPaging(this.innerHTML)

Or maybe the id:

getPaging(this.id);

However, it's easier (and better practice) to add the click handlers from JavaScript code, and not include them in the HTML. Seeing as you're already using jQuery, this can easily be done by changing your HTML to:

<li class="clickMe">1</li>
<li class="clickMe">2</li>

And use the following JavaScript:

$(function () {
    $('.clickMe').click(function () {
        var str = $(this).text();
        $('#loading-content').load('dataSearch.php?' + str, hideLoader);
    });
});

This will add the same click handler to all your <li class="clickMe">s, without requiring you to duplicate your onclick="getPaging(this.value)" code for each of them.

like image 40
David Tang Avatar answered Oct 06 '22 00:10

David Tang


I prefer to use the HTML5 data API, check this documentation:

  • https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
  • https://api.jquery.com/data/

A example

$('#some-list li').click(function() {
  var textLoaded = 'Loading element with id='
         + $(this).data('id');
   $('#loading-content').text(textLoaded);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id='some-list'>
  <li data-id='1'>One </li>
  <li data-id='2'>Two </li>
  <!-- ... more li -->
  <li data-id='n'>Other</li>
</ul>

<h1 id='loading-content'></h1>
like image 37
fitorec Avatar answered Oct 06 '22 00:10

fitorec