shin
shin

Reputation: 32721

How to find substring of href with JavaScript?

I have the following output.

<tr id='row_27' valign='top'>
...
...

<td width="5%"><a href="http://localhost/daikon2/index.php/projects/admin/delete_log/6/6/27" class="deleteme"><img src="http://localhost/daikon2/assets/icons/delete.png"  alt="delete"  title="Delete Log" /></a></td>
</tr>

I want to get the last segments 27 in the href with JavaScript.

I tried the following but nothing happened.

var id =href.substring(href.lastIndexOf("/") + 1);
                        alert (id);

Upvotes: 0

Views: 3535

Answers (1)

Tim Down
Tim Down

Reputation: 324657

Firstly, how are you getting the href variable? Here's one way:

var href = document.getElementById("row_27").getElementsByTagName("a")[0].href;

I would suggest using the slice method of the string, which takes a substring from the index to the end of the string if you specify only one parameter:

var id = href.slice(href.lastIndexOf("/") + 1);
alert(id);

Upvotes: 3

Related Questions