-1

just for increase my knowledge, i am asking this question to you. do you think, its possible to attach the value in html elements like php.

let brief the example. in php.

 <img src="<?php echo $imgSource ?>" title="<?php echo $imgSourceTitle ?>" >

can we do same in JavaScript or JQuery.
something like this.

<img src="javascript:$(this).val(imgSrc)" title="javascript:$(this).val(imgSrcTitle)" >
Sudhir K Gupta
  • 65
  • 1
  • 11

1 Answers1

2

It's not possible to attach the values like that, because JavaScript code is not parsed like PHP and should be executed once the DOM is ready. To change DOM elements you can use jQuery inside the $(document).ready()function. Try something like this:

$(document).ready(function()
{
    $("#my_a").attr("href","http://google.it");
    $("#my_a").attr("title", "my_title");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="my_a">Test</a>

Note that the code is executed once the page is completely loaded, not at parsing time (like PHP does).

halfer
  • 19,824
  • 17
  • 99
  • 186
Marco Moschettini
  • 1,555
  • 2
  • 16
  • 26