4

I have a problem in passing one of html element to jQuery. I know that with attr() we can access to attributes. Nevertheless I feel I do something wrong.

This is my simple jQuery function:

<script type="text/javascript">
    function test(value) {
        alert(value);
    }
</script>


I have a dynamic listbox which has been created by foreach loop:

<select  multiple="multiple" name="factors1" id="main_factors" style="width: 200px; height: 200px;">

<?php foreach ($array as $option): ?>
    <option onclick="test(<script>this.attr('title').value</script>);"
            id="<?php echo $option[0]; ?>"
            title="<?php echo $option[2]; ?>" 
            value="<?php echo $option[0]; ?>">
        <?php echo $option[1]; ?>
    </option>
<?php endforeach ?>

</select>

I want when somebody click on items in list box its item show in my messagebox for example. However this code did not work for me.

onclick="test(<script>this.attr('title').value</script>);"

How can I send title attribute value to my function from onclick attribute?

Thanks in advance.

4 Answers 4

2
onclick="test(this)"

function test(value)
{
    alert($(value).attr('title'));
}

see demo

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

1 Comment

@user108: onchange event is better than onclick events while using select elements
2

Try this code. I think you make a mistake in your code.

<select  multiple="multiple" name="factors1" id="main_factors" style="width: 200px; height: 200px;" onchange="test(this.value);">

<?php foreach ($array as $option): ?>
<option id="<?php echo      $option[0]; ?>" title="<?php echo $option[2]; ?>" 
value="<?php  echo $option[0]; ?>"><?php echo $option[1]; ?></option>
<?php endforeach ?>


</select>

And in your jQuery:

<script type="text/javascript">
    function test(value)
       {
        alert(value);
       }
    </script>

2 Comments

onchange event is better than onclick events while using select elements
onchange event should be defined on <select> so it will capture value of option when changed. See my answer.
0

You should need to use onchange event on select element.

​<select onchange="test(this.value)">

JavaScript:

function test(value) {
    alert(value)        
}​

SEE DEMO

Comments

0

Since you have imported and are utilizing jQuery, it is better practice to separate your event handlers from your html elements, as a sort of separation of design from functionality:

Remove the onclick altogether from your php:

<select  multiple="multiple" name="factors1" id="main_factors" style="width: 200px; height: 200px;">
<?php foreach ($array as $option): ?>
<option id="<?php echo $option[0]; ?>" title="<?php echo $option[2]; ?>" 
value="<?php  echo $option[0]; ?>"><?php echo $option[1]; ?></option>
<?php endforeach ?>
</select>

And bind a function to the change event from within the javascript:

$(document).ready(function() {
    $('#main_factors').change( function() {
        test( $(this).val() );
        // you can also do:
        // test( this.value );
    });
});

function test(value)
{
    alert(value);
}

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.