Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 30
  1. #11

    Default Re: Hack: Display a text in Select that is not Found in options.


    Quote Originally Posted by kamahak View Post
    It is probably because of your definition of what a hack is. Mao nang wa ka kasabot. What you probably are basing this on is hacking that is portrayed by media. IE, getting into computers, defacing websites, etc.

    In actuality, hacking, is a mindset. A mindset that entices you to think out of the box to make something (anything) do what it isn't supposed to.

    In this case, the hack is to make html 'select' element show different text when you choose an option. In the example, it was supposed to show Volvo or Harley. Instead, when you select it, it shows: Cars, Volvo or Motorcycles, Harley.

    @TS- Nindot pagkahimo. Tan-awn nya nakog tarong ang code puhon.
    first, mao pd ba.. wa pd ko ka realize na mao iya pasabot.. tubag pd tarong ang bata.. hehehe..
    second, unsay @TS bai? lamats..

  2. #12

    Default Re: Hack: Display a text in Select that is not Found in options.

    Ambot, Topic Starter mn tingai pasabot anang TS. Basta kibaw ko basta @TS kay pasabot ang Author sa thread. So, Topic Starter tingai pasabot ana. Hehe. Sunod2x, rmn ko dri. haha.

    Wa ko kbantay naa na diay naka-una ug butang ug jQuery version. Hehe.

    Anyways, here's my version.

    HTML:
    HTML Code:
    <select>
      <optgroup label="Car">
          <option>Volvo</option>
          <option>Saab</option>
          <option>Mercedes</option>
          <option>Audi</option>
      </optgroup>
      <optgroup label="Motorcycle">
          <option>Harley</option>
          <option>Suzuki</option>
          <option>Honda</option>
          <option>Ninja</option>
      </optgroup>   
    </select>
    JS:
    Code:
    $("select").change(function (event) {
        var foo = $("select option:selected")
        , bar = foo.text()
        , cat = foo.parent().attr("label")
        , old = $("select option:contains('" + cat + "')")
        , oldbar = old.html();
        
        oldbar && old.html(oldbar.replace(cat + ': ',''));
        foo.text(cat + ': ' + bar);
    });
    Naay duha ka bugs. Inig click nmu kay makita nmu ang "Car: " sa option. Ug kung mu click ka from one group to another kay mabilin ang "group: " na tag. Gamay ra au ko oras gd pra mka trabaho ani. Bday sa ako uyab, manlakaw pmi. Haha.

    Ugma tan-awn nko balik.

  3. #13

    Default Re: Hack: Display a text in Select that is not Found in options.

    My head was full of GIGO when I looked at the code, but when I looked at the the output, I was like, daaaamnnnn why was that so hard to understand

  4. #14

    Default Re: Hack: Display a text in Select that is not Found in options.

    cool guys.. thanks for all your jQuery versions and comments and either a hack or whatever you call it, it's still cool right? hehehe.. @kahamahak: abi ko thread starter hehe.. chuy chuy.. anyways.. i have a question for you guys, unsa na event ang gamiton para naa kay script mapa run when selecting an already selected option in a select?

    things i've tried so far:

    1. Tried to put events on the option tag, drawback is not all browsers support events in option tag. As far as i know, only FF supports it ( onClick ), everything else, nada.
    2. onChange in select tag doesn't fire so wa gypn na.
    3. Drinked coffee and rethinked steps, wa gypn.. @_@.

  5. #15
    Elite Member noy.juan's Avatar
    Join Date
    Mar 2010
    Gender
    Male
    Posts
    1,521
    Blog Entries
    1

    Default Re: Hack: Display a text in Select that is not Found in options.

    Quote Originally Posted by kamahak View Post
    Ambot, Topic Starter mn tingai pasabot anang TS. Basta kibaw ko basta @TS kay pasabot ang Author sa thread. So, Topic Starter tingai pasabot ana. Hehe. Sunod2x, rmn ko dri. haha.

    Wa ko kbantay naa na diay naka-una ug butang ug jQuery version. Hehe.

    Anyways, here's my version.

    HTML:
    HTML Code:
    <select>
      <optgroup label="Car">
          <option>Volvo</option>
          <option>Saab</option>
          <option>Mercedes</option>
          <option>Audi</option>
      </optgroup>
      <optgroup label="Motorcycle">
          <option>Harley</option>
          <option>Suzuki</option>
          <option>Honda</option>
          <option>Ninja</option>
      </optgroup>   
    </select>
    JS:
    Code:
    $("select").change(function (event) {
        var foo = $("select option:selected")
        , bar = foo.text()
        , cat = foo.parent().attr("label")
        , old = $("select option:contains('" + cat + "')")
        , oldbar = old.html();
        
        oldbar && old.html(oldbar.replace(cat + ': ',''));
        foo.text(cat + ': ' + bar);
    });
    Naay duha ka bugs. Inig click nmu kay makita nmu ang "Car: " sa option. Ug kung mu click ka from one group to another kay mabilin ang "group: " na tag. Gamay ra au ko oras gd pra mka trabaho ani. Bday sa ako uyab, manlakaw pmi. Haha.

    Ugma tan-awn nko balik.
    you need to use focus event, to reset the option element when selected by the use and before the change event us trigger

    this is how the flow should works in jQuery and non-jQuery

    focus => change => blur

    btw, TS = Thread Starter or kung kinsa nag himo sa thread...

  6. #16

    Default Re: Hack: Display a text in Select that is not Found in options.

    simplified version of my original code:

    Code:
    var _originalList = '';
    var _selectedIndex = 0;
    
    function changeList(obj){
        if ( obj.selectedIndex != 0 ){
            _selectedIndex = obj.selectedIndex;
            option = obj[_selectedIndex];
            parent = option.parentElement
    
            if(!parent){
                parent =  option.parentNode
            }
            option.text = parent.label+', '+obj.value;
            obj.blur();
        }
    }
    
    function showOriginalList(obj){
        if ( _originalList == '' ){
            _originalList = obj.innerHTML;
        }
        obj.innerHTML = _originalList;
        obj.selectedIndex = _selectedIndex; 
    }
    HTML Code:
    <select id=sel onchange="changeList(this,'change');" onfocus="showOriginalList(this);">
      <option>--- Please Select --- </option>
      <optgroup label="Car">
      <option onChange="alert(this.text);">Volvo</option>
      <option>Saab</option>
      <option>Mercedes</option>
      <option>Audi</option>
      </optgroup>
      <optgroup label="Motorcycle">
      <option>Harley</option>
      <option>Suzuki</option>
      <option>Honda</option>
      <option>Ninja</option>
      </optgroup>   
    </select>
    • Bug to fix: unable to change the displayed text for already selected option.
    • e.g. if you selected "Ninja" for example, it will be displayed as "Motorcycle, Ninja". if you select it again, no change.
    @noy.juan: SAKTO gyud imo analysis sako code bai.. and glad you clarified the TS thing..

  7. #17

    Default Re: Hack: Display a text in Select that is not Found in options.

    Hello again. finally remembered to fix this.
    Quote Originally Posted by noy.juan View Post
    you need to use focus event, to reset the option element when selected by the use and before the change event us trigger

    this is how the flow should works in jQuery and non-jQuery

    focus => change => blur

    btw, TS = Thread Starter or kung kinsa nag himo sa thread...
    Yeah, I know it would be easier to use the focus() event. But, IMHO, the beauty of jQuery is to be able to shorten JavaScript code as much as possible. As they're infamous mantra goes, "write less, do more".

    Anyways, on second look of my code. The problem was obviously that my code was not remembering the last category when switching between the two. Thus, creating a bug. So, I made the appropriate fixes.

    Code:
    $("select").change(function () {
        var oldc;
        return function (event) {
            var foo = $("select option:selected"),
                bar = foo.text(),
                cat = foo.parent().attr("label");
            if (oldc) { // This won't exist the first time we run this
                oldfoo = $("select option:contains('" + oldc + ":')");
                oldHTML = oldfoo.html();
                oldHTML && oldfoo.html(oldHTML.replace(oldc + ': ', ''));
            }
            foo.text(cat + ': ' + bar);
            oldc = cat; // Remember it for next time
        };
    }());
    JSFiddle Linkage

    By this time, I was having so much fun, I was just staring blankly at the screen when; BAM!

    Epiphany.

    The profound solution was not to search for or save the last category but to use other special characters found on the list. That's right the colon.

    The result, a beautiful jQuery four-liner.

    Code:
    $("select").change(function (event) {
        var foo = $("select option:selected"),
            bar = foo.text(),
            cat = foo.parent().attr("label"),
            old = $("select option:contains(':')"),
            oldb = old.html();
    
        oldb && old.html(oldb.replace(oldb.substr(0, oldb.indexOf(':') + 2),''));
        foo.text(cat + ': ' + bar);
    });
    JSFiddle Linkage

    Note: I say four-liner because this script is basically just four lines of code. Just edited for readability.

    Thank you TS for taking away the boring times.

  8. #18

    Default Re: Hack: Display a text in Select that is not Found in options.

    Quote Originally Posted by kamahak View Post
    It is probably because of your definition of what a hack is. Mao nang wa ka kasabot. What you probably are basing this on is hacking that is portrayed by media. IE, getting into computers, defacing websites, etc.

    In actuality, hacking, is a mindset. A mindset that entices you to think out of the box to make something (anything) do what it isn't supposed to.

    In this case, the hack is to make html 'select' element show different text when you choose an option. In the example, it was supposed to show Volvo or Harley. Instead, when you select it, it shows: Cars, Volvo or Motorcycles, Harley.

    @TS- Nindot pagkahimo. Tan-awn nya nakog tarong ang code puhon.
    i pretty much know what is hacking...i just don't understand what the TS wants to show

  9. #19

    Default Re: Hack: Display a text in Select that is not Found in options.

    Quote Originally Posted by nullpointer View Post
    i pretty much know what is hacking...i just don't understand what the TS wants to show
    Quote Originally Posted by kamahak
    In this case, the hack is to make html 'select' element show different text when you choose an option. In the example, it was supposed to show Volvo or Harley. Instead, when you select it, it shows: Cars, Volvo or Motorcycles, Harley.
    Umm..This?

  10. #20

    Default Re: Hack: Display a text in Select that is not Found in options.

    Quote Originally Posted by kamahak View Post
    Umm..This?
    guys guys.. make peace not war..

  11.    Advertisement

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

 
  1. Looking For: Lawyer that is an expert in annulment
    By Jason4eveR in forum Specialty Services
    Replies: 3
    Last Post: 06-21-2018, 01:28 PM
  2. Why Financial Education is not taught in school?
    By *dudes* in forum Campus Talk
    Replies: 39
    Last Post: 10-27-2010, 05:55 PM
  3. Looking For: Sony Ericsson K550i or k800i that is cheap and in perfect condition..
    By JeffCee in forum Cellphones & Accessories
    Replies: 13
    Last Post: 08-02-2009, 12:10 PM
  4. Why Financial Education is not taught in school?
    By *dudes* in forum Politics & Current Events
    Replies: 22
    Last Post: 11-23-2008, 03:10 PM
  5. Replies: 8
    Last Post: 01-14-2006, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top