Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery dynamic id

i use such code to access item

function f(id){

$("#"+id).val(); // with analogy $("#id item")
}

is it correct? is any other methods?

like image 781
kusanagi Avatar asked Feb 26 '23 23:02

kusanagi


1 Answers

If you want to return the value of an element with specified id, then yes as that is what seems to be logical purpose of your function:

function f(id){
  return $("#" + id).val();
}

The functions should assume that an element with specified id exists and then it returns you the value of that element. This should work for input fields as well as textarea. If however, it is any other element, you might want to use html() or text() instead of val() eg:

function f(id){
  return $("#" + id).html();
  // return $("#" + id).text();
}
like image 178
Sarfraz Avatar answered Mar 12 '23 03:03

Sarfraz