0

I have the below html element.And i want to apply the src value it has for the attribute data-val-required to another element's(id='Title2') src property.

<input id="Title" name="Title" type="text" data-val-required="<img class='validateicon'
       src='https://mysprt/Content/Images/16x16-red-alert.png'/><font color='Red'>*</font> Required" >

I tried the below and it works but i want to know if there is a better approach than this ?

javascript:alert($($('#Title').attr('data-val-required')).attr('src'));
krrishna
  • 2,032
  • 4
  • 44
  • 99

3 Answers3

0

Use data attribute instead:

$($('#Title').data('val-required')).attr('src');

Demo

Milind Anantwar
  • 79,642
  • 23
  • 92
  • 120
0

Use .data() : return the value at the named data store of the selector.

.attr() can be suggested only in some specific cases - Reason

$($('#Title').data('val-required')).attr('src');

Or

$($('#Title').data('valRequired')).attr('src');

.data() automatically camelCases the '-'

Or

$($('#Title').data('val-required')).prop('src');

Fiddle Demo

Community
  • 1
  • 1
Shaunak D
  • 20,236
  • 10
  • 45
  • 76
0

Try this :

$(document).ready(function() 
{    
   alert($($('#Title').data('val-required')).attr('src'));
});

Working JSFiddle

Bhushan Kawadkar
  • 27,908
  • 5
  • 34
  • 57