SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I want only certain buttons to be displayed through jquery

    I have a grid of buttons ehere user onpens up the gird by clicking on the (Open Grid) link and then clicking on a button in that grid. What will then happen is that a set of buttons appear below the text box depending on the button selected from the grid. So if user select button "1" then button "A" appears, if user selects button "2" then buttons "A" and "B" appears and so on.

    Now this is done through jquery code below:

    $(".gridBtns").click(function() {

    var clickedNumber = this.value;

    $('.answerBtns').each(function (index) {
    if (index < clickedNumber)
    $(this).show();
    else
    $(this).hide();
    });

    });

    But what I want to do is that if user selects button "True or False" from the grid, I want only the buttons "True" and "False" under the text box and if user selects "Yes or No" button from the gird, then I only want buttons "Yes" and "No" to display under the text box, How can this be do in my jquery code?

    Also if user selects "True or False" button or "Yes or No" button, then I want the "Number of Answers" text box to disappear and display the span tag "N/A" only, does anyone know to do this in jquery.

    Thanks and code is in jsfiddle, click [here][1]


    [1]: http://jsfiddle.net/pSy97/10/

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by malcolm p View Post
    But what I want to do is that if user selects button "True or False" from the grid, I want only the buttons "True" and "False" under the text box and if user selects "Yes or No" button from the gird, then I only want buttons "Yes" and "No" to display under the text box, How can this be do in my jquery code?
    One way to do this would be to split the job up in to two separate tasks.
    The first task would be to check if the button is true/false, or yes/no, and to then show the appropriate buttons.

    The first thing to do is to get rid of the inline event attribute
    Code javascript:
    if (clickedNumber === 'True or False') {
        // show true/false buttons
        ...
    } else if (clickedNumber === 'Yes or No') {
        // show yes/no buttons
        ...
    } else {
        // show numeric buttons
        ...
    }

    Quote Originally Posted by malcolm p View Post
    Also if user selects "True or False" button or "Yes or No" button, then I want the "Number of Answers" text box to disappear and display the span tag "N/A" only, does anyone know to do this in jquery.
    Yes indeed. With a slight readjustment to the above conditional checks, both can be relatively easily catered for.


    Code javascript:
    if (Number(clickedNumber)) {
        // show "Number of Answers" and remove any possible N/A span tag
        ...
        // show numeric buttons
        ...
    } else {
        // hide "Number of Answers" and show N/A span tag
        ...
        if (clickedNumber === 'True or False') {
            // show true/false buttons
            ...
        } else if (clickedNumber === 'Yes or No') {
            // show yes/no buttons
            ...
        }
    }

    Now that the clickedNumber variable is going to contain more than just a number, and is in truth a string from this.value, have you considered renaming it to a more appropriate variable name, perhaps buttonValue or gridButton or something like that?

    If so, with some refactoring to keep the related pieces of code together, it could be:

    Code javascript:
    if (Number(buttonValue)) {
        // show numeric buttons
        ...
    } else if (buttonValue === 'True or False') {
        // show true/false buttons
        ...
    } else if (buttonValue === 'Yes or No') {
        // show yes/no buttons
        ...
    }
    if (Number(buttonValue)) {
        // show "Number of Answers" and remove any possible N/A span tag
        ...
    } else {
        // hide "Number of Answers" and show N/A span tag
        ...
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •