1

All,

I've a javascript routine which returns a string value, I want to use this routine inside my html tags like this,

<a href="#"><script>getKeyValue('empDesignation')</script></a>

Is this possible?. Any insights?

2
  • can't you use something like document.write? Commented Apr 11, 2012 at 14:31
  • Hi Sam, Can u check see my update latest edit? Commented Apr 11, 2012 at 14:32

5 Answers 5

2
<html>
  <head>

<script type="text/javascript">
function alterText(){
document.getElementById('hello').innerHTML = empDesignation();
}
  function empDesignation(){   
    var output = 'test'
   return output
  }
</script>

  </head>
  <body onload="javascript:alterText()">

    <a id="hello"></a>

  </body>


  </html>

This should work. I had to mock up your empDesignation function.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Sam, I want to do this without using DHTML. Is there any way available?
I don't think so. Not if you're getting your value from a javascript function. I'll add the html into my example if you like.
1

document.write(getKeyValue('empDesignation')); This would probably do the trick.

UPDATE: It has to be enclosed within <script> tags

2 Comments

Hi Dominik, When I try to write like that, it my javascript menu gets disappeared, and the designation alone gets printed in the browser.
document.write will erase everything before writing if the page has already been finished loading, innerHTML should be used instead
0

no you cant use javascript variables inside html tags. You have to set the value to html by using DHTML.

If you post some code i can help you

1 Comment

Hi Yorgo, I want to do this without using DHTML. Is there any way available?
0

You can do

var txt = getKeyValue('empDesignation');
$('#result').text(txt);

This assumes that getKeyValue() returns a string. Then you put that the result as text of element with id=result

EDIT - after your edit. you could do

HTML

<a href="#" data-key='empDesignation' class='functional'>your text</a>

js

//when you click on a link that has the class functional    
$('a.functional').on('click', function(e){
  //prevent the default action
  e.preventDefault();
  //retrieve the data that from the html5 data attribute and pass it to 
  //your custom function
  var result = getKeyValue($(this).data('key'));
  //use the resulting string as the new text for the clicked link
  $(this).text(result);
})

4 Comments

Hi Nicoloa, If I do like this, my menu size tends to shrink.
@EdsonMedina i know i thought it was a user defined function. I added the missing dot
@EdsonMedina getKeyvalue() is a custom function.
Thanks Nicoloa!.. Good solution!.. but I'm not dealing the with anchor click events.
0

inside html tags this getKeyValue('empDesignation') it is not possible,

use innerHTML like below example

           <html>
    <script type="text/javascript">
    function changeText(){
        document.getElementById('boldStuff').innerHTML = 'is Software Developer';
    }
    </script>
    <body>
    <p>Employee Designation <b id='boldStuff'>is what?</b> </p> 
    <input type='button' onclick='changeText()' value='Change Text'/>
    </body>
    </html>

EDIT

          <script type="text/javascript">
    function changeText(){
             var get_des=getKeyValue('empDesignation');

            document.getElementById('change_id').innerHTML = get_des;
    }
    </script>

           <a href="#" id="change_id" onclick='changeText()'></a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.