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,050
  • 4
  • 47
  • 101

3 Answers3

0

Use data attribute instead:

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

Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
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,588
  • 10
  • 46
  • 79
0

Try this :

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

Working JSFiddle

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57