473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

DOM onclick action

Hello everyone,

I'm a fairly new to javascript and I need some help regarding element
IDs.
What I need is a simple javascript action that is attached to a
checkbox that upon checking and unchecking changes the id= attribute of
the button named search (name="search")

initially the button is checked and id should be set at "searchbutton"
(id="searchbutton")
when the checkbox is unchecked I want id of that button to change:
id="button".

I searched the web for few hours and haven't found any examples.
here's my approximate scenario

if (document.myform.mycheckbox.checked == true) {
somehow change button.id to "searchbutton" }
else if (document.myform.mycheckbox.checked == false)
change button.id to "button" }

I'm not even sure if changing id of the element is possible since I was
trying to use getElementById and then the ID changes.

Any suggestions? If possible a short example is appreciated.

Regards,

Peter

Aug 19 '06 #1
2 1399
redpixel wrote:
Hello everyone,

I'm a fairly new to javascript and I need some help regarding element
IDs.
What I need is a simple javascript action that is attached to a
checkbox that upon checking and unchecking changes the id= attribute of
the button named search (name="search")
I am completely at a loss as to why you want to do that, but anyway...

initially the button is checked and id should be set at "searchbutton"
(id="searchbutton")
when the checkbox is unchecked I want id of that button to change:
id="button".

I searched the web for few hours and haven't found any examples.
here's my approximate scenario

if (document.myform.mycheckbox.checked == true) {
somehow change button.id to "searchbutton" }
else if (document.myform.mycheckbox.checked == false)
change button.id to "button" }
Guessing that the "search" button is in the same form (and wrapping for
posting):

document.myform.search.id =
(document.myform.mycheckbox.checked)? 'searchbutton' : ' button';
I'm not even sure if changing id of the element is possible since I was
trying to use getElementById and then the ID changes.
Why you want to change it? An element's id property is accessible and
changeable as a normal JavaScript object property using either dot or
square bracket notation. You could also use setAttribute, but it is
buggy in some browsers.

If you don't know which ID to use to get a reference to the element
known occasionally as 'searchbutton' and otherwise as 'button, you could
try:

var buttonRef;
if (document.getElementById('searchbutton')){
buttonRef = document.getElementById('searchbutton');
} else {
buttonRef = document.getElementById('button');
}

Which can be shortened to:

var buttonRef = (document.getElementById('searchbutton') ||
document.getElementById('button'));
--
Rob
Aug 19 '06 #2

RobG wrote:
redpixel wrote:
Hello everyone,

I'm a fairly new to javascript and I need some help regarding element
IDs.
What I need is a simple javascript action that is attached to a
checkbox that upon checking and unchecking changes the id= attribute of
the button named search (name="search")

I am completely at a loss as to why you want to do that, but anyway...

initially the button is checked and id should be set at "searchbutton"
(id="searchbutton")
when the checkbox is unchecked I want id of that button to change:
id="button".

I searched the web for few hours and haven't found any examples.
here's my approximate scenario

if (document.myform.mycheckbox.checked == true) {
somehow change button.id to "searchbutton" }
else if (document.myform.mycheckbox.checked == false)
change button.id to "button" }

Guessing that the "search" button is in the same form (and wrapping for
posting):

document.myform.search.id =
(document.myform.mycheckbox.checked)? 'searchbutton' : ' button';
I'm not even sure if changing id of the element is possible since I was
trying to use getElementById and then the ID changes.

Why you want to change it? An element's id property is accessible and
changeable as a normal JavaScript object property using either dot or
square bracket notation. You could also use setAttribute, but it is
buggy in some browsers.

If you don't know which ID to use to get a reference to the element
known occasionally as 'searchbutton' and otherwise as 'button, you could
try:

var buttonRef;
if (document.getElementById('searchbutton')){
buttonRef = document.getElementById('searchbutton');
} else {
buttonRef = document.getElementById('button');
}

Which can be shortened to:

var buttonRef = (document.getElementById('searchbutton') ||
document.getElementById('button'));
--
Rob


Thank you Rob!

Your advice with

document.myform.search.id =
(document.myform.mycheckbox.checked)? 'searchbutton' : ' button';

was enough to make the form working.
The reason I wanted to change id of the submit button is because
"searchbutton" was the id to use with ajax live-search. but the script
was constructed in the unobstrusive manner, to demonstrate that normal
$_GET through url can be used when ajax live-search is disabled.
Changing button ID to something else (like "button") would stop ajax
from executing live search and page would reload giving search results.
Thanks for your help.

Peter

Aug 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zalek Bloom | last post by:
Hello, Here is my problem: I am displaying to a user a row of buttons. Depending on which button user presses, I want to go to a different URL and to give a different value to a hidden...
4
by: Michael Hill | last post by:
I had this html: <tr id="action" title="click to do something" onclick="alert('mike');"> <td>a</td> <td>b</td> </tr> and it works when I click on the row, but when I try to add it...
2
by: kelvin | last post by:
Hi, I've this piece of code which does not work at all. Can anyone point out my mistake? I've 2 buttons. History button will call verifyFields() function and lead to different page for...
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
7
by: RHPT | last post by:
I have a checkbox that I dynamically check (via JavaScript) when the user initiates an action on the page. I want to prevent the user from unchecking the box if that action has been initiated and...
3
by: Michael_R_Banks | last post by:
I'm trying to dynamically build a table that allows users to remove rows when they click a corresponding button. For some reason, whenever I add the button to the table, it never fires the onclick...
1
by: rkhurana | last post by:
Hi I have a <ui:button> setup with onClick and action. See below. The problem is that if I return true in the onClick, then the action handler gets called but not the updateChart(). If I return...
31
by: ajos | last post by:
hi frnds, i have a form,which has 2 input text boxes, the values are entering the text boxes,when i leave the 2 text boxes blank and hit submit a java script gives the message that the 2 fields are...
4
by: since | last post by:
How do I in IE prevent the onclick action from being fired when I am done dragging? have tried "window.event.cancelBubble = true", for onmouseup , onmousedown, and onmousemove handlers. The onclick...
1
by: Jacqui | last post by:
Hi, I am trying to write a webpage that has a form on it, which gets submitted when a button is pressed, but also has an onclick event which does some DOM manipulation to display a waiting page....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.