/    Sign up×
Community /Pin to ProfileBookmark

Change that <span>

How can I change this code to make it when someone doesn’t fill out a field correctly change the text for the field that is in a span tag. Right now it changes the color of the span to red, but I want to dynamically add a *

////////////////////////////////////JAVASCRIPT PART/////////////////
function chkName(form) {
var fullname = document.contactForm.name.value;
var name = document.contactForm.name.value.indexOf(” “, 0);

if(document.contactForm.name.value == “”) {
alert(“Please enter your name”);
document.getElementById(“tag1”).style.color = “red”;
document.contactForm.name.focus();

return false;
}

////////////////////////////////////////////////HTML//////////////////
<span id=”tag1″>Name:</span><br />
<input type=”text” size=”25″ name=”name” style=”border: 0″ /><br />

Any help would be appreciated big time.

[url]www.momaddidley.com[/url]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisOct 05.2004 — document.getElementById("tag1").innerHTML = "*" + document.getElementById("tag1").innerHTML;
Copy linkTweet thisAlerts:
@madddidleyauthorOct 05.2004 — That add the * but the script is putting it before the text. I tried to flip the code you gave me but that gave me an error before any javascript was loaded.
Copy linkTweet thisAlerts:
@agent_x91Oct 05.2004 — try:


document.getElementById("tag1").innerHTML +="*";
Copy linkTweet thisAlerts:
@gil_davisOct 05.2004 — You were not clear where you wanted the "*".

document.getElementById("tag1").innerHTML = document.getElementById("tag1").innerHTML + "*";should work. Or
document.getElementById("tag1").innerHTML += "*";
Copy linkTweet thisAlerts:
@madddidleyauthorOct 05.2004 — Rock on!! Thanks fuch much
Copy linkTweet thisAlerts:
@madddidleyauthorOct 06.2004 — I have modified my page so that it adds a * behind the form field. But now my script doesn't set the focus() to that field. What should I change or what could I do?


//////////////////////////////////////////////////////////////////////

function chkName(form) {

var fullname = document.contactForm.name.value;

var name = document.contactForm.name.value.indexOf(" ", 0);

if(document.contactForm.name.value == "") {

alert("Please enter your name");

document.getElementById("tag1").focus();//////////THIS DOESN'T WORK

document.getElementById("tag1").style.color = "orange";

document.getElementById("tag1").innerHTML += " *";

return false;

}





//////////////////////////////////////////////////////////////////////

<div id="tag1">Name:<br />

<input type="text" size="25" name="name" style="border: 0" /></div>




Thanks in advance.






www.momadddidley.com
Copy linkTweet thisAlerts:
@javaNoobieOct 06.2004 — shouldnt it be
document.contactForm.name.focus();
you cant set focus to a <span>
Copy linkTweet thisAlerts:
@madddidleyauthorOct 06.2004 — for some reason that isn't working. Would the fact that the field is inside a div. Oh yeah, I changed the span to a div also. But yes the focus() isn't working.
Copy linkTweet thisAlerts:
@gil_davisOct 06.2004 — You should not use reserved words for identifiers. There is already a parameter called "name" for a form field, so the browser probably gets confused. Name the form field something else and "focus()" on that.
×

Success!

Help @madddidley spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...