EzDevInfo.com

onclick interview questions

Top onclick frequently asked interview questions

Click toggle with jQuery

I've used a hover function where you do x on mouseover and y and mouseout. I'm trying the same for click but it doesn't seem to work:

$('.offer').click(function(){ 
  $(this).find(':checkbox').attr('checked', true ); 
},function(){
  $(this).find(':checkbox').attr('checked', false ); 
});

I want the checkbox to be checked when clicking on a div, and unchecked if clicked again - a click toggle.


Source: (StackOverflow)

Onclick javascript to make browser go back to previous page?

Is there a function I can attach as a click event of a button to make the browser go back to previous page?

<input name="action" type="submit" value="Cancel"/>

Source: (StackOverflow)

Advertisements

Can I have an onclick effect in CSS?

I have an image element that I want to change on click.

<img id="btnLeft">

This works:

#btnLeft:hover {
    width:70px;
    height:74px;
}

But what I need is:

#btnLeft:onclick {
    width:70px;
    height:74px;
}

But, it doesn't work, obviously. Is it possible at all to have onclick behavior in CSS (i.e. without using JavaScript)?


Source: (StackOverflow)

How to call multiple JavaScript functions in onclick event?

Is there any way to use the onClick html attribute to call more than one JavaScript function?


Source: (StackOverflow)

Trigger a button click with JavaScript on the Enter key in a text box

I have one text input and one button (see below). How can I use JavaScript to trigger the button's click event when the Enter key is pressed inside the text box?

There is already a different submit button on my current page, so I can't simply make the button a submit button. And, I only want the Enter key to click this specific button if it is pressed from within this one text box, nothing else.

<input type="text" id="txtSearch" />
<input type="button" id="btnSearch" value="Search" onclick="doSomething();" />

Source: (StackOverflow)

How exactly does the android:onClick XML attribute differ from setOnClickListener?

From that I've read you can assign a onClick handler to a button in two ways.

Using the android:onClick XML attribute where you just use the name of a public method with the signaturevoid name(View v) or by using the setOnClickListener method where you pass an object that implement the OnClickListener interface. The latter often requires an anonymous class which personally I don't like (personal taste) or defining an internal class that implements the OnClickListener.

By using the XML attribute you just need to define a method instead of a class so I was wondering if the same can be done via code and not in the XML layout.


Source: (StackOverflow)

Calling a parent window function from an iframe

I want to call a parent window JavaScript function from an iframe.

<script>
    function abc()
    {
        alert("sss");
    }
</script>

<iframe id="myFrame">
    <a onclick="abc();" rel='nofollow' href="#">Call Me</a>
</iframe>

Source: (StackOverflow)

How to simulate a button click through code in Android?

How can I trigger a button click event through code in Android? Basically, I want to trigger the button click programmatically when some other event occurs.

Thanks a lot :)

Same Problem I am Facing

public void onDateSelectedButtonClick(View v){
    /*Something  Alarm Management 
    http://www.java2s.com/Code/Android/Core-Class/Alarmdemo.htm
    copied code from this site*/
}

Button code:

<Button
    android:onClick="onDateSelectedButtonClick"
    android:text="Set notification for this date" />

But I want to call that function OnLoadLayout without OnClickEvent I tried a lot of different ways, but I cannot do without button click anybody please help me


Source: (StackOverflow)

How to click or tap on a TextView text

I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.

I keep thinking about button listeners and anonymous method listener calls, but it just does not seem to apply to TextView.

Can someone point me at some code snippet to show how clicking or tapping on a piece of text in a TextView runs a method?


Source: (StackOverflow)

How can I simulate an anchor click via jquery?

I have a problem with faking an anchor click via jQuery: Why does my thickbox appear the first time I click on the input button, but not the second or third time?

Here is my code:

<input onclick="$('#thickboxId').click();" type="button" value="Click me" />

<a id="thickboxId" rel='nofollow' href="myScript.php" class="thickbox" title="">Link</a>

It does always work when I click directly on the link, but not if I try to activate the thickbox via the input button. This is in FF. For Chrome it seems to work every time. Any hints?


Source: (StackOverflow)

Remove an onclick listener

I have an object where the text cycles and displays status messages. When the messages change, I want the click event of the object to change to take you to the activity that the message is relating to.

So, I have a TextView mTitleView and I'm assigning the event like this.

public void setOnTitleClickListener(OnClickListener listener) {
    mTitleView.setOnClickListener(listener);
}

How do I remove that click event? There are some status messages that do not have an actionable area so I'd like to turn off the click event. I'd also like to be able to cycle through these click events and dispose of them properly, but I'm unsure of the best practice.


Source: (StackOverflow)

Attaching click event to a JQuery object not yet added to the DOM

I've been having a lot of trouble attaching the click event to a JQuery object before adding it to the DOM.

Basicly I have this button that my function returns, then I append it to the DOM. What I want is to return the button with its own click handler. I don't want to select it from the DOM to attach the handler.

My code is this:

createMyButton = function(data) {

  var button = $('<div id="my-button"></div>')
    .css({
       'display' : 'inline',
       'padding' : '0px 2px 2px 0px',
       'cursor' : 'pointer'
     }).append($('<a>').attr({
       //'href' : Share.serializeJson(data),
       'target' : '_blank',
       'rel' : 'nofollow'
     }).append($('<image src="css/images/Facebook-icon.png">').css({
       "padding-top" : "0px",
       "margin-top" : "0px",
       "margin-bottom" : "0px"
     })));

     button.click(function () {
        console.log("asdfasdf");
     });

     return button;     
}

The button that is return is unable to catch the click event. However, if i do this (after the button is added to the DOM):

$('#my-button').click(function () {
    console.log("yeahhhh!!! but this doesn't work for me :(");
});

It works... but not for me, not what I want.

I've spent hours trying to make this work, it seems to be related to the fact that the object is not yet a part of the DOM. Any help would be very apreciated.

Oh! By the way, I'm working with OpenLayers, and the DOM object that I'm appending the button to is an OpenLayers.FramedCloud (Wich is not yet a part of the DOM but will be once a couple of events are triggered.)


Source: (StackOverflow)

OnClick vs OnClientClick for an asp:CheckBox?

Does anyone know why a client-side javascript handler for asp:CheckBox needs to be an OnClick="" attribute rather than an OnClientClick="" attribute, as for asp:Button?

For example, this works:

<asp:CheckBox runat="server" OnClick="alert(this.checked);" />

and this doesn't (no error):

<asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />

but this works:

<asp:Button runat="server" OnClientClick="alert('Hi');" />

and this doesn't (compile time error):

<asp:Button runat="server" OnClick="alert('hi');" />

(I know what Button.OnClick is for; I'm wondering why CheckBox doesn't work the same way...)


Source: (StackOverflow)

$(document).click() not working correctly on iPhone. jquery

This function works perfectly on IE, Firefox and Chrome but when on the iPhone, it will only work when clicking on a <img>. Clicking on the page (anywhere but on a img) wont fire the event.

$(document).ready(function () {
  $(document).click(function (e) {
    fire(e);
  });
});

function fire(e) { alert('hi'); }

The HTML part is extremely basic and shouldnt be a problem.

Any ideas?


Source: (StackOverflow)

How to have click event ONLY fire on parent DIV, not children?

I have a DIV with a classed "foobar", and a few DIVs inside that DIV that are unclassed, but I suppose they are inheriting the "foobar" class:

$('.foobar').on('click', function() { /*...do stuff...*/ });

I want that to fire off only when clicking somewhere in the DIV but not on its children DIVs.


Source: (StackOverflow)