I use the following code to display the user name on the website.. like
"Welcome Kaushik"

<jsp:useBean id="adminUser" class="bss.BSSUserDetailsDo" scope="session"/>

<font size="4" color="brown" face="verdana">
<bean:message key="Bss.user.welcome"/> //comes from some property file  abcd.property
<jsp:getProperty name="adminUser" property="firstName"/>
</font>

i want to access the user first name from my JS function.
i tried

var name = document.getElementById('firstName');

but not able to access! any idea..

Put the text data in their own container tags. Also don't use <font> tags, they are deprecated in favor of stylesheets. Something like :

<jsp:useBean id="adminUser" class="bss.BSSUserDetailsDo" scope="session"/>
<div style='font: normal 12px verdana; color: brown;'>
<bean:message key="Bss.user.welcome"/>
<span id='firstName'><jsp:getProperty name="adminUser" property="firstName"/></span>
</div>

You were erroneously using the bean property 'firstName' to access the element of the DOM. You have got to realize that the server side constructs you are using have got nothing to do with the generated text. The user only gets the processed JSP file.

thanx for your response S.O.S.. i have made the changes as you told.
now when i try to access it and display on my page using " name.value " it displays "undefined". What i might have done wrong??

i saw my page source code after executing the page.. the source is some thing like this

<div style='font: normal 12px verdana; color: brown;'>
Welcome
<span id="firstName">
Tommy 
</span>
</div>

where Tommy is the name i want to acces.Do you think its correct?

hay another thing..

this is a new thing which came with the change..

i made a function such that if the user types the same name as "Tommy" it should execute.
And the funny thing is as soon as i press "t" or "T" it gets executed.

friend while i was trying i found the following code also give the same problem.. why is it so. If the code below works then my problem will be solved!

<html>
<body>

<h1 id="header">My header</h1>

<script type="text/javascript">
document.getElementById('header').style.color="red"
var a = document.getElementById('header');
document.write(a.value);
</script>

<p><b>Note:</b> It is the script that changes the style of the element!</p>

</body>
</html>

i changed the code from http://www.w3schools.com/dhtml/tryit.asp?filename=trydhtml_dom_color

Friend i was able to solve my problem using JSP within JS but i will wait for the answer ! I want to learn!
ThanX

<div style='font: normal 12px verdana; color: brown;'>
Welcome<span id="firstName">Tommy</span>
</div>

Access the contents of non-form elements(span and div) using the ' innerHTML ' property. Something like document.getElementById('firstName').innerHTML > And the funny thing is as soon as i press "t" or "T" it gets executed.
You need to show me the event handling code.

> var a = document.getElementById('header'); > document.write(a.value); Again the same mistake. Access the inner HTML of a non-form element using the ' innerHTML ' property. Something like alert(a.innerHTML).

ok.. i didnot knew this.. Thanx for your responses.. Let me try.thanx again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.