0

I have a basic question involving CSS and JavaScript.

I have a page with several DIVs, each one with an unique ID taken from a mysql database. When a user clicks on a certain DIV, I want that one to disappear from the page. Because everything it's dynamic my solution was to pass the id as a variable to the function. The problem is I don't know how use that id. I need something to replace document.getElementById().

JS:

function flashout(the_unique_id){
document.getElementByID(???).style.display = "";
}

HTML/CSS:

<div id="394" onclick="flashout(394)">...</div>
<div id="723" onclick="flashout(723)">...</div>
4
  • 5
    What's wrong with document.getElementByID(the_unique_id)? Commented Aug 26, 2013 at 21:33
  • 2
    And id can't be a number: ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). Commented Aug 26, 2013 at 21:37
  • 3
    @Edward: Those were HTML4 restrictions. Anyway, I'm pretty sure JavaScript implementations still successfully fetched by numeric id. Commented Aug 26, 2013 at 21:38
  • 1
    @LightStyle: That you've misspelled it: …Id :-) Commented Aug 26, 2013 at 22:15

2 Answers 2

5

The ID should work but you don't have to use it. Just pass this as a parameter:

<div id="394" onclick="flashout(this)">...</div>
<div id="723" onclick="flashout(this)">...</div>

and use that param as a element itself instead of string ID:

function flashout(div_element){
   div_element.style.display = "none";
}

Oh and if you want DIV to disappear - set style.display to "none", not "".

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

Comments

1

I think I understand what you are looking for.

When you you are establishing a function and its parameters in JavaScript, the parameter can be used like a variable while defining the function. Try this

HTML:

<div id="394" onclick="flashout(394)">...</div>
<div id="723" onclick="flashout(723)">...</div>

JS:

function flashout(unique_id){
  document.getElementByID(unique_id).style.display = "";
}

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.