简体   繁体   中英

How to get attr value in jquery

I am trying to get value of attr " _last_val " from my input but unable to produce it.

Below is ? i have tried demo

//below is HTML

<form method="post" action="" id="feedback_form">
  <input type="text" value="2014-08-11" class="date" name="add_by_datetime" _last_val="2014-08-14" >
  <input type="button" name="submit_now" value="Submit" /> 
</form>

// below is script

jQuery(function($) {
   $("form#feedback_form input[name='submit_now']").on("click",function() { 
      var actualadddate  = $("form#feedback_form input[name='add_by_datetime']").attr('_last_val');
      alert(+actualadddate+'aaaaaaaaa');    
   }); 
});

please let me know where i am wrong.

Thanks

Remove + operator from beginning. Use:

alert(actualadddate+"aaaaaaaaa");

Demo

Remove the preceded + from alert and try,

alert(actualadddate + 'aaaaaaaaa');

Live working demo

Note that, in your example you are using .date class to access the attribute if your page has more than 1 element having same class then it will not give you the accurate date. So, be carefull in that case or use unique id to get the attribute.

because of the + before actualadddate its converted to a number, that results in NaN ( Not a Number )

so, remove it

alert(actualadddate+'aaaaaaaaa');

http://jsfiddle.net/59o60g7e/10/

Since, actualadddate is not a number, it thows NaN ie not a number. Remove + from the alert which you used to typecast.

Use this instead,

alert(actualadddate+"aaaaaaaaa");

Also, instead of using user defined attributes. Use data attribute to store your custom values.

The only thing wrong in your code is the alert call. http://jsfiddle.net/rcnw1op0/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM