-1

So I want to Substring the selected attribute, but i get the error: "TypeError: src is undefined"

Heres the Code:

js

function reset_select(){
 var src = $('td.onedit').prop('id');
 var new_stuhl = src.substr(1, 1);
 ...
}

Html:

<tr>    
<td id="s1.1" class="names onedit"><p class="name">Name Name</p></td>
</tr>

console.log(src) gives me the string 's1.1' and typeOf is 'string'. When I change

var src = "s1.1";

it works. I'm using jQuery 3.2.1.

Why does this not work? Is there soething wrong with the selector?

servollo
  • 3
  • 3

1 Answers1

1

You are required to have a <table> tag around your <tr> tags.

As mentioned by @SandeepNayak, you can read more about why in this question: How do browsers analyze <tr> <td> without <table>?

It works fine with it:

function reset_select(){
     var src = $('td.onedit').prop('id');
     var new_stuhl = src.substr(1, 1);
     
     console.log(src);
     console.log(new_stuhl);
}

reset_select();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>    
        <td id="s1.1" class="names onedit"><p class="name">Name Name</p></td>
    </tr>
</table>
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 1
    [This](https://stackoverflow.com/questions/23333870/how-do-browsers-analyze-tr-td-without-table) could be helpful as well – Sandeep Nayak Jun 01 '17 at 07:33