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"/>

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..

9 Years
Discussion Span
Last Post by kaushik259106

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>

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;'>
<span id="firstName">

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!


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

<script type="text/javascript">
var a = document.getElementById('header');

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


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!

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

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).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.