-1

Jquery:

$datasearch.append('<tr>'
    +'<td class="edit-btn" id='+"tid"+' onClick='+"pic()"+'  value='+"hhh"+'></td>'+
    +'</tr>');

function:

<script type="text/javascript">
 function pic() {
    var x = document.getElementById("tid").value;
    alert(x);
 }
</script>

Error: alert Undefined

isherwood
  • 58,414
  • 16
  • 114
  • 157
iMatti
  • 13
  • 6
  • 3
    `id='+"tid"+'` <-- ????? Not to mention that `td` elements don't have a `value` property/attribute, they have `.textContent` and `.innerHTML`. I think you should start by cleaning up your code to make it valid. – Scott Marcus Oct 30 '19 at 21:20
  • There's not much description here, nor really a good way of reproducing this. Can you please create a [mre]? You can use [Stack Snippets](https://meta.stackoverflow.com/q/358992/215552) to make sure it runs. Note that concatenating a string to add an event handler is a pretty old method; you could just use jQuery's [`on`](https://api.jquery.com/on/) method to do it instead... – Heretic Monkey Oct 30 '19 at 21:24
  • Possible duplicate of [JavaScript, getting value of a td with id name](https://stackoverflow.com/questions/2310145/javascript-getting-value-of-a-td-with-id-name) – Heretic Monkey Oct 30 '19 at 21:26
  • @HereticMonkey i want to get value="" property, example which you have given is not returning value. can you please help how to fetch only value. – iMatti Oct 30 '19 at 21:36
  • As mentioned, there isn't anything in the question. Please [edit] your question to make it clear what exactly your question is. As for the answer, a hint: `getAttribute`. – Heretic Monkey Oct 30 '19 at 21:42
  • Why does this have the `jquery` tag if you're using vanilla JavaScript? – Barmar Oct 30 '19 at 21:52
  • What is the exact error message? `alert Undefined` doesn't look an error I've seen. – Barmar Oct 30 '19 at 21:52

1 Answers1

-1

Use 'data' attribute instead 'value'

$datasearch.append(
`<tr>
   <td class="edit-btn" id="tid" data="hhh">hhh</td> 
</tr>`);

Function

<script type="text/javascript">
 $(()=> {
    $('body').on('click', '#tid', function() {
      let $this = $(this);
      let x = $this.data() || $this.attr('data');
      alert(x);
    }
 });
</script>
  • 1
    You've added template literals, arrow functions, unnecessary short-circuited assignments, and changed event binding, while adding an event namespace. You've also corrected the invalid HTML - -- All without any explanation. This may work, but at Stack Overflow, we want to have people understand our answers. – Scott Marcus Oct 30 '19 at 21:44